Search code examples
phpyii2

How to manage routes in Yii2 when 'urlManager' array does not exist?


I have inherited a Yii2 application to maintain, and although I use plain PHP and Laravel, I do not know enough about this framework. Therefore, I feel very insecure and confused at times.

For example, the application manages the backend and frontend separately, as if it were a multisite application.

On the frontend side, there is a urlManager array in config/main.php, in which all public routes are defined.

The backend, on the other hand, is in a subdomain, and there is no definition of routes in any configuration file (/common/config/* or /backend/config/* ), so I cannot understand how to generate new routes for this area.

So I need to create a new set of routes for backend area and I can't figure how to achieve it

App tree is like the following

├── backend
│   ├── assets
│   │   └── resources
│   ├── components
│   │   ├── SupplierController.php
│   │   ├── SupplierDetailViewer.php
│   │   ├── WebController.php
│   │   └── views
│   ├── config
│   │   ├── bootstrap.php
│   │   ├── main-local.php
│   │   ├── main.php
│   │   ├── params-local.php
│   │   └── params.php
│   ├── controllers
│   │   ├── ...
│   │   ├── SiteController.php
│   │   ├── SupplierController.php
│   │   ├── TermsOfUseController.php
│   │   ├── UserController.php
│   │   └── base
│   ├── gii
│   │   └── SupplierWorkGiiantCRUD.json
│   ├── models
│   │   ├── ...
│   │   └── SupplierUserAccessReportForm.php
│   ├── runtime
│   │   ├── CSS
│   │   ├── HTML
│   │   ├── URI
│   │   ├── cache
│   │   └── logs
│   ├── views
│   │   ├── ...
│   │   ├── layouts
│   │   ├── site
│   │   ├── supplier
│   │   ├── supplier-user
│   │   ├── user
│   │   └── ...
│   └── web
│       ├── assets
│       ├── cropper
│       ├── favicon.ico
│       ├── images
│       ├── index.php
│       └── robots.txt
├── common
│   ├── components
│   │   ├── CDNCalculator.php
│   │   ├── HighCharts.php
│   │   ├── LanguageSelector.php
│   │   └── Yii.php
│   ├── config
│   │   ├── bootstrap.php
│   │   ├── main-local.php
│   │   ├── main.php
│   │   ├── messages.php
│   │   ├── params-local.php
│   │   └── params.php
│   ├── gii
│   │   ├── ...
│   │   ├── admin_settingGiiantModel.json
│   │   ├── tSupplierGiiantModel.json
│   │   └── ...
│   ├── interfaces
│   │   └── ...
│   ├── models
│   │   ├── ...
│   │   ├── Supplier.php
│   │   ├── SupplierWork.php
│   │   ├── TSupplier.php
│   │   ├── User.php
│   │   ├── UserHasProveedorSupportTicketCategory.php
│   │   ├── base
│   │   ├── query
│   │   ├── search
│   │   └── validators
│   └── widgets
│       ├── ...
│       ├── GMapInput.php
│       ├── OneScroll.php
│       ├── gmapinput
│       ├── onescroll
│       └── views
├── composer.json
├── console
│   ├── config
│   │   ├── bootstrap.php
│   │   ├── main-local.php
│   │   ├── main.php
│   │   ├── params-local.php
│   │   └── params.php
│   ├── controllers
│   │   ├── CropperMantainerController.php
│   │   ├── ExportController.php
│   │   ├── FakeDataController.php
│   │   ├── ImportController.php
│   │   └── nohup.out
│   ├── models
│   └── runtime
│       ├── cache
│       └── logs
├── docker-compose.yml
├── environments
│   ├── dev
│   │   ├── backend
│   │   ├── common
│   │   ├── console
│   │   ├── frontend
│   │   └── yii
│   ├── dev-docker
│   │   ├── backend
│   │   ├── common
│   │   ├── console
│   │   ├── frontend
│   │   └── yii
│   ├── index.php
│   ├── prod
│   │   ├── backend
│   │   ├── common
│   │   ├── console
│   │   ├── frontend
│   │   └── yii
│   └── test
│       ├── backend
│       ├── common
│       ├── console
│       ├── frontend
│       └── yii
├── frontend
├── init
├── init.bat
├── nginx
│   ├── backend
│   ├── frontend
│   └── nginx.conf
├── yii
└── yii.bat


Solution

  • In Yii 2, the fact that there is no urlManager config in backend/config/main.php doesn't mean that urlManager is not used. It just means that it's used with its default settings.

    If you want to you can simply add urlManager config array in components part of backend/config/main.php.

    But in most cases having urlManager component with its default settings is enough for backend because there is no need to optimize urls for SEO.

    In its default settings the urlManager expects rules to be in format like this:

    backend.domain.example/index.php?r=<route>
    

    Where route can be in formats like this:

    <controller>/<action>
    <controller>/<action>/<id>
    <module>/<controller>/<action>
    <module>/<controller>/<action>/<id>
    

    As long as your urls follow these patterns you don't need to explicitly declare routes for each controller/action.