codejunkie
codejunkie

Reputation: 77

How to set alias for a non web-acessible folder in Yii2

Hi I made a module in my Yii2 basic application. I need a path to display an image in my view, but I can't get the path right. My web-accessible directory is located in C:/xampp/htdocs/basic and only containing Yii2 entry script and published assets folder. All my application files are located in a non web-accessible directory C:/xampp/htdocs/gbia, which is in the same directory level (or some might call it 'sibling directory') with the folder where my Yii2 entry script and published assets are located.

I made an upload path param in my model and it's getting called by the method from the controller.

I tried these in separate occasion after a research :

Yii::$app->params['uploadUrl'] = Yii::$app->urlManager->baseUrl . '/uploads/batam/';
Yii::$app->params['uploadUrl'] = Yii::$app->request->baseUrl  . '/uploads/batam/';
Yii::$app->params['uploadUrl'] = Yii::getAlias('@web').'/uploads/batam/';

and also Url::base(), Url::base(true) But they all didn't return my baseUrl. They only echoed out /uploads/batam/, without the base URL, as if the base Url returns NULL.

lastly, I tried echoing Yii::$app->homeUrl; and it returns /

I'm still learning and as to my knowledge, @web refer to the directory where our index.php entry script was, please correct me if I'm wrong. So I tried to solve the problem by making a new alias in my config :

'aliases'=>[
'@batam'=> Yii::$app->getAlias('@webroot') . '/../gbia/uploads/batam',
],

and also tried with getAlias('@web') but they both return this error : Uncaught Error: Call to a member function getAlias() on null

I also tried this in my aliases:

'@batam'=> Yii::$app->basePath.'/../gbia/uploads/batam',

but also failed. Can anyone help? Thanks!

Upvotes: 0

Views: 1979

Answers (2)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23778

The aliases @webroot and @web are not available in the config file. The reason being the application run after requiring the config file and the aliases are set in the in the bootstrap() of the yii\web\Application. Look into your entry script file web\index.php if you are using basic app it should be like below

<?php

// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../config/web.php';

(new yii\web\Application($config))->run();

and you can see that the config file is required before initializing the application, hence the aliases @webroot and @web not being set yet are not accessible.

So what yo uneed to do is you have to set the aliases to be used in the config file and for that you can do the following

  • Create a file named config/aliases.php.

  • Set the required aliases in the file. Yii::setAlias('@webroot',dirname(dirname(__DIR__)).'/web');

  • Require the above file in the entry script index.php before the config/web.php file and that's it now you can access the alias in the config file, your entry script should now look like below


<?php

// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/../config/aliases.php';
$config = require __DIR__ . '/../config/web.php';

(new yii\web\Application($config))->run();

Update

For uploading the images to outside the document root i would suggest that you make a symlink inside the webroot which will be pointing to your folder outside the document root, and point your alias to that symlink, this way you would be saving the images outside the document root and wont have to go through all this.

Upvotes: 1

Sanya
Sanya

Reputation: 57

You need to register it in bootstrap.php file

Yii::setAlias('@webroot', dirname(dirname(__DIR__)) . '/path/to/webroot');

Upvotes: 0

Related Questions