Renaming index.php breaks console ? Problem understanding alias config maybe?

I have a basic application with a few pages and commands tested and working.

Now I want to move it around in the file system.

I want to share the webroot with another php system.

So I rename index.php to y.php, and hard-code the paths:


<?php

require('/home/me/sites/ppv/vendor/autoload.php');

require('/home/me/sites/ppv//vendor/yiisoft/yii2/Yii.php');

$config = require('/home/me/sites/ppv/config/web.php');

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

Looks like it works, I can navigate the basic app ok.

But there are problems…

  1. A previously created alias is no longer available to me. I broke it. But how?

Then I notice this line at the top of config/console.php:


Yii::setAlias('@tests', dirname(__DIR__) . '/tests/codeception');

That seems odd. Why does it not use the alias config?

I add my own alias in a similar manner and poof it is available. Now I am confused. Wht did the configuration stop working?

Then I have trouble running some console tasks.


me: /yii cron/serial/test

BEGIN TEST

PHP Fatal error:  Class 'app\common\components\Fcmd' not found in /home/me/sites/ppv/commands/cron/SerialController.php on line 60



So I know Yii is finding a controller nested in /commands/cron/SerialController and invoking the action.

But what happened to my FCmd component in /common/components?


    

namespace app\commands\cron;


use Yii;

use \yii\console\Controller;

use hank\hankDump;

use hank\hankKernal;

use app\models\kvs\Serials;

use app\models\kvs\Videos;

use app\common\components\Fcmd;  /** EXPERIMENTAL **/


class SerialController extends TaskController    

{

...

public function actionTest()

    {

        echo "BEGIN TEST\n";


        $ffcmd = new Fcmd();  /** FAILED **/

        $ffcmd = new \app\common\components\Fcmd(); /** FAILED **/



I have also added this line to my nginx config:


index index.php y.php index.html;

…but pretty urls fail with 404s. I do not care, I do not neeed pretty urls. But is that telling something?

Possibly related question:

Changing the name of index.php seems to be supported. Looks like Yii figures it out by itself.

Just the same, I find no documentation for what the value of ‘ScriptUrl’ should be in the UrlManager config.

Is it even relevant? Does it want the full protocol+domain+entry script, or is it just the entry script?

Not sure what I am looking for. I have renamed the enrty script for the web application.

Why has the console app broken?

I was careful to put my shared component in the common folder.

Here is the console config…




<?php

Yii::setAlias('@tests', dirname(__DIR__) . '/tests/codeception');

Yii::setAlias('@hank', '/home/me/sites/hank');/** THIS CORRECTS THE BROKEN CONFIG BELOW ****/


$params = require(__DIR__ . '/params.php');

$db = require(__DIR__ . '/db.php');


$config = [

    'id' => 'basic-console',

    'basePath' => dirname(__DIR__),

    'bootstrap' => ['log'],

    'controllerNamespace' => 'app\commands',

    'components' => [

        'cache' => [

            'class' => 'yii\caching\FileCache',

        ],

        'log' => [

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

        'db' => $db,

        'ffcmd' => [

            'class' => 'app\components\ffmeg',   /*** I BROKE THIS ***/

        ],

    ],

    'aliases' => [

        '@hank' => '/home/me/sites/hank', /*** I BROKE THIS ***/


    ],

    'params' => $params,

];

return $config;

Finally I should mention I am still pretty new to Yii and php namespaces. Learning fast but never fast enough…

Thank you for any advice.

Hi, As you are using nginx, there is no need for this complicated hard coding. You can change your nginx configuration and make two server variables - one for your yii application, another for your other php app. In these you can declare the root variable which will be the web root for the respective applications.

All the best!

Thank you for your suggestion. Same domain, same port (443 ssl), same root, how does nginx know who is who? Will require some nginx smarts. Would have to be a different location or a reverse proxy …?

Consider hard coding is a convenience as I try to find what works. Those paths can be refactored as relative in the usual way. DIR . '…/…'etc

If I want to keep using Yii2, I better come to understand what is happening in php land.

The makers of Yii2 have set an alias (in the config file!) instead of using the config itself. Why does that work? Why does it matter in my case?

And why the heck can I not load my component? I don’t think I want to paper over my ignorance with a nginx fix which is equally out of reach of my tiny mind anyway.

If you can shed any more light on your proposed solution please do. I guess putting the Yii webroot in a subfolder of the main webroot is the best I can do today.

Thanks again. Still open to guidance here.

I tracked this down to a problem with camel case and hyphens ( eg SiteController::actionThisThing <==> site/this-thing)

Still could not explain it or solve it.

Started a new project, did it all over (it was not much), and avoided camel case and hyphens.

No problem now.

But no understanding gained. Win some lose some.