Routing and modules - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Routing and modules (/showthread.php?tid=78044) |
Routing and modules - lucky - 11-24-2020 I'm trying to separate my project in different modules. I've the following modules - admin - portal - site The main controller is site, there is where users land for the first time. Here are my routes on Modules\Site\Config\Routes.php PHP Code: /** This works perfectly. When I request the home page or any other controller on that namespace works without issues. If I request /register or /recover or /about it works. The problem starts when trying to use any of the other modules, let's use admin as an example. This is my Routes.php file inside the admin module. (Modules\Admin\Config\Routes.php) PHP Code: /** The root of admin works well, so if I try to access the home works. Doing http://myurl.com/admin or http://myurl.com/admin/index What I don't understand is why http://myurl.com/admin/login won't work, I'm just calling one of the methods inside the Home controller in admin. I always get the following error Code: 404 - File Not Found Debugging that I get Code: string(34) "\Modules\Lobby\Controllers\Admin" string(5) "login" Clearly it's not finding Admin::login because the namespace is wrong, but all that is set on Routes.php config file. Two clarifications:
RE: Routing and modules - InsiteFX - 11-24-2020 You need to add all 3 of those to the app/Confog/Autoload.php psr4 section. PHP Code: public $psr4 = [ Something like that. Insitefx/Admin/Config/Routes.php The routes are stored in the modules Config folder. PHP Code: <?php RE: Routing and modules - lucky - 11-25-2020 In app/config/Autoload.php I have this PHP Code: public $psr4 = [ If for the module site all the routes work with something as simple as this PHP Code: /** Why can I do the same for admin? Without creation every specific rule. Setting a default namespace, controller and method will route automatically to http://site.com/home/login http://site.com/home/welcome http://site.com/register http://site.com/activate http://site.com/recover ... etc But for admin, I need to specify every single rule? It does not make any sense. My admin module has the following in it PHP Code: // We get a performance increase by specifying the default So shouldn't every url with admin before it be automatically mapped? So if I have URLs like this http://site.com/admin/login http://site.com/admin/home http://site.com/admin/users http://site.com/admin/pages http://site.com/admin/tools they should be automatically working without me defining each one of them. after all they follow the pattern controller::method RE: Routing and modules - InsiteFX - 11-25-2020 The namespaces and paths should be the same, word for word. PHP Code: 'Modules\Admin' => ROOTPATH . 'Modules/Admin', RE: Routing and modules - lucky - 11-26-2020 Thanks for your help InsiteFX, but I think it's not possible :/ https://codeigniter4.github.io/userguide/general/modules.html Under the section Controllers it says Quote:Controllers outside of the main app/Controllers directory cannot be automatically routed by URI detection, but must be specified within the Routes file itself: RE: Routing and modules - InsiteFX - 11-26-2020 it is possible because I'm using routes for my blog I'm working on. Here is my routes in Insitefx/Blog/Config/Routes.php PHP Code: <?php namespace Insitefx\Blog\Config; RE: Routing and modules - lucky - 11-26-2020 Yes, I understand, but you have to define them. If you work on app/controllers the routes work automatically without needing to define them. If you work on modules/some_module/controllers you have to define the route. I have tried your solution and does not work. Even the CI guide says the same, that controllers outside the main app folder won't work :/ I think the only solution is create a symlink from app/controllers to each module so app/controllers/admin -> modules/admin/controllers app/controllers/portal -> modules/portal/controllers app/controllers/users -> modules/users/controllers I don't see other way. What you wrote below, is what I want to avoid. The need to define every single route. PHP Code: <?php namespace Insitefx\Blog\Config; RE: Routing and modules - InsiteFX - 11-26-2020 Then make sure that you set this to true. PHP Code: $routes->setAutoRoute(true); You can use route_to() to go to the route. PHP Code: <?= route_to('blog')'?> |