Problem with routes if Controller is in a subfolder |
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 !
I think (: segment) shouldn't have that extra space. Like (egment) instead of (: segment)
Website: marcomonteiro.net | Blog: blog.marcomonteiro.net | Twitter: @marcogmonteiro | TILThings: tilthings.com
If the controller is in a sub-folder you also need to pass the sub-folder name
sub-folder-name/controller/method What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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');
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');
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...
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.
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 ?
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. |
Welcome Guest, Not a member yet? Register Sign In |