Search code examples
phpurlyiiyii2url-routing

Yii2 How can I route the current Url to controller outside the root path of the current project?


I have 2 websites projects on the main directory inside /var/www/ directory what I want to do is creating controller with it's views to be shared between the 2 project, not repeat the same controller on both projects, now I create it ex. myController.php inside host A

How can I access the controller from the second host B ? and render myaction function?

the url rules in the main.php config file 'newpage/list'=>'myController/myaction',

Edit :: I'm using this advanced template

**DIRECTORY STRUCTURE**
common
    config/              contains shared configurations
    mail/                contains view files for e-mails
    models/              contains model classes used in both backend and frontend
    tests/               contains tests for common classes    
console
    config/              contains console configurations
    controllers/         contains console controllers (commands)
    migrations/          contains database migrations
    models/              contains console-specific model classes
    runtime/             contains files generated during runtime
backend
    assets/              contains application assets such as JavaScript and CSS
    config/              contains backend configurations
    controllers/         contains Web controller classes
    models/              contains backend-specific model classes
    runtime/             contains files generated during runtime
    tests/               contains tests for backend application    
    views/               contains view files for the Web application
    web/                 contains the entry script and Web resources
frontend
    assets/              contains application assets such as JavaScript and CSS
    config/              contains frontend configurations
    controllers/         contains Web controller classes
    models/              contains frontend-specific model classes
    runtime/             contains files generated during runtime
    tests/               contains tests for frontend application
    views/               contains view files for the Web application
    web/                 contains the entry script and Web resources
    widgets/             contains frontend widgets
vendor/                  contains dependent 3rd-party packages
environments/            contains environment-based overrides

Question is : How I can access the front end controllers in front end directory from backend rules ?


Solution

  • Frontend and Backed are two different modules. When they are bootstrapping from index.php they behave like two individual projects. So, you cannot route from frontend to backed or vise versa using urlManager of Yii.

    May be you can maintain some params in common/params where you can configure absolute Url.