![]() |
Problem with routes if Controller is in a subfolder - 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: Problem with routes if Controller is in a subfolder (/showthread.php?tid=76874) Pages:
1
2
|
Problem with routes if Controller is in a subfolder - kilden - 06-28-2020 Hello, I don't know if it's a good practice, but I wanted to put the controllers for my admin in a subfolder called 'admin' to keep it tidy. So I've changed the namespace like this... Code: namespace App\Controllers\admin; ... copied the base controller in my admin folder (that's probably the problem) ...And change my routes like this... Code: $routes->post('/admin/mycontroller', 'admin/Mycontroller::mymethod'); Everything worked well till I wanted to pass arguments... It works like this : Code: $routes->match(['get','post'], '/admin/translations/(: segment)', 'Admin_translations::translate/$1'); It doesn't work like this : Code: $routes->match(['get','post'], '/admin/translations/(: segment)', 'admin/Translations::translate/$1'); What should I do to fix this issue ? Thank you for any help ! RE: Problem with routes if Controller is in a subfolder - marcogmonteiro - 06-29-2020 I think (: segment) shouldn't have that extra space. Like ( ![]() RE: Problem with routes if Controller is in a subfolder - kilden - 06-29-2020 (06-29-2020, 01:26 AM)marcogmonteiro Wrote: I think (: segment) shouldn't have that extra space. Like ( you're right... In my code, there was no space... I put one intentionally to see the code because on this forum, ": + s" makes a smiley... RE: Problem with routes if Controller is in a subfolder - InsiteFX - 06-29-2020 If the controller is in a sub-folder you also need to pass the sub-folder name sub-folder-name/controller/method RE: Problem with routes if Controller is in a subfolder - kilden - 06-29-2020 I think it's exactly what I did just below... My sub-folder is 'admin', my controller 'Translations' and my method 'translate': (06-28-2020, 09:04 PM)kilden Wrote: It doesn't work like this : I put the name of the sub-folder in the route, but when passing arguments, it doesn't work and the controller or method is not found ! ![]() Or should I put a "/" instead "::" between controller and method ? $routes->match(['get','post'], '/admin/translations/(: segment)', 'admin/Translations/translate/$1'); RE: Problem with routes if Controller is in a subfolder - inumaru - 06-30-2020 By not working, what is it the error that you got? is it exception or 404 or what? Do you have a folder in your public by the same name "admin"? if you have it try to rename it into something else, and try this code in your routes file. PHP Code: $routes->match(['get','post'], 'admin/translations/(:alpha)', 'admin/Translations::translate/$1'); RE: Problem with routes if Controller is in a subfolder - kilden - 07-01-2020 Yes. Even with your code I've got a white page with a 404 error + "admin" like this : Code: <div class="wrap"><h1>404 - File Not Found</h1> I'm surprised there is no more information because I'm in development mode. Everything works well if I replace my argument (for exemple : 'myfield') in the route : Code: //$routes->match(['get','post'], 'admin/translations/(:alpha)', 'admin/Translations::translate/$1'); //doesn't work But of course, I need to put arguments... RE: Problem with routes if Controller is in a subfolder - inumaru - 07-01-2020 Do you have a folder in your public by the same name "admin"? I got an 403 error last time because of this. or maybe how are the contents of your routes file and Translations file? I use sub-folders in my controller too but it works just fine. RE: Problem with routes if Controller is in a subfolder - kilden - 07-01-2020 Well I've changed a bit the structure according to my host server and to put easily a project on my website if I need to show my local project on my website for a client... (maybe it would be easier to do a subdomain ?) So for the moment, I've got : CI-projectName (my app folder) ----app --------Controllers -------------admin (the folder for my admin controllers) --------Views -------------pages (the folder for my webpages views) -------------pages-admin (the folder for my admin views) ----system ----writable web (the public folder) ----projectName (name of the app for testing : myDomain/projectName) --------index.php --------assets -------------css -------------js --------assets-admin -------------css -------------js Do you mean I should better change my structure ? RE: Problem with routes if Controller is in a subfolder - inumaru - 07-03-2020 Sorry I just able to go online and check this again. I have tried to replicate your folder structure and all your route and controller syntax, and I found something interesting. PHP Code: $routes->match(['get','post'], 'admin/translations/(: segment)', 'admin/Translations::translate/$1'); --> not working(404) Yet PHP Code: $routes->match(['get','post'], 'admin/translations/someVar', 'admin/Translations::translate/someVar'); --> working I just relize the difference between your code and my code is this \ and / that being used. If no variable is being used, using / is fine but if there is variable / will resulting in 404 and only \ works. |