Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 620 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get root directory in yii2

#1
**yii2 Question**

My yii2 install in `d:\wamp\www\yii2store`

I want to get above path to save images which will be uploaded by me or users.

I have pass all available arguments in `Yii::getAlias('@webroot')` (below are the lists of argument which I have used).

`@yii` - framework directory.

`@app` - base path of currently running application.

`@runtime` - runtime directory.

`@vendor` - Composer vendor directory.

`@webroot` - web root directory of currently running web application.

`@web` - base URL of currently running web application.

And also once I will get above path to save images then how can I get path something like this `localhost/yiistore2/upload` to be use in img tag src.

One more thing how can I create my own alias with `Yii::setAlias()` and where to create this so that I can load it on every controller.
Reply

#2
To get the base URL you can use this (would return "http:// localhost/yiistore2/upload")

Yii::app()->baseUrl

The following Code would return just "localhost/yiistore2/upload" without http[s]://

Yii::app()->getBaseUrl(true)


Or you could get the webroot path (would return "d:\wamp\www\yii2store")

Yii::getPathOfAlias('webroot')
Reply

#3
Try out this,

My installation is at D:\xampp\htdocs\advanced

`\Yii::$app->basePath` will give like `D:\xampp\htdocs\advanced\backend`.

`\Yii::$app->request->BaseUrl` will give like `localhost\advanced\backend\web\`


You may store the image using `\Yii::$app->basePath` and show it using `\Yii::$app->request->BaseUrl`


Reply

#4
Use "getAlias" in Yii2

\Yii::getAlias('@webroot')
Reply

#5
Open below file `C:\xampp\htdocs\project\common\config\params-local.php`

Before your code:

<?php

return [


];

after your code:

<?php
yii::setAlias('@path1', 'localhost/foodbam/backend/web');

return [


];

Reply

#6
Supposing you have a writable "uploads" folder in your application:

You can define a param like this:

Yii::$app->params['uploadPath'] = realpath(Yii::$app->basePath) . '/uploads/';

Then you can simply use the parameter as:

$path1 = Yii::$app->params['uploadPath'] . $filename;

Just depending on if you are using advanced or simple template the base path will be (following the [link](

[To see links please register here]

) provided by phazei):

> Simple @app: Your application root directory
>
> Advanced @app: Your application root directory (either frontend or
> backend or console depending on where you access it from)

This way the application will be more portable than using `realpath(dirname(__FILE__).'/../../'));`
Reply

#7
Open file
`D:\wamp\www\yiistore2\common\config\params-local.php`

Paste below code before return

Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));

After inserting above code in params-local.php file your file should look like this.

Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));

return [
];

Now to get path of your root (in my case its `D:\wamp\www\yiistore2`) directory you can use below code in any php file.

echo Yii::getAlias('@anyname');
Reply

#8
If you want to get the root directory of your yii2 project use, assuming that the name of your project is **project_app** you'll need to use:

echo Yii::getAlias('@app');

on windows you'd see "C:\dir\to\project_app"

on linux you'll get "/var/www/dir/to/your/project_app"

I was formally using:

echo Yii::getAlias('@webroot').'/..';

I hope this helps someone
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through