• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with routes if Controller is in a subfolder


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...
namespace App\Controllers\admin;

... copied the base controller in my admin folder (that's probably the problem)

...And change my routes like this...

$routes->post('/admin/mycontroller', 'admin/Mycontroller::mymethod');

Everything worked well till I wanted to pass arguments...

It works like this :
$routes->match(['get','post'], '/admin/translations/(: segment)', 'Admin_translations::translate/$1');

It doesn't work like this :
$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 (Confusedegment) instead of (: segment)

(06-29-2020, 01:26 AM)marcogmonteiro Wrote: I think (: segment) shouldn't have that extra space. Like (Confusedegment) instead of (: segment)

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...

If the controller is in a sub-folder you also need to pass the sub-folder name

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 :
$routes->match(['get','post'], '/admin/translations/(:alpha)', 'admin/Translations::translate/$1');

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 !  Undecided

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 :

<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 : 

//$routes->match(['get','post'], 'admin/translations/(:alpha)', 'admin/Translations::translate/$1'); //doesn't work
$routes->match(['get','post'], 'admin/translations/myfield', 'admin/Translations::translate/myfield'); //works!!
strange... :huh:
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)
-------------admin (the folder for my admin controllers)
-------------pages (the folder for my webpages views)
-------------pages-admin (the folder for my admin views)
web (the public folder)
----projectName (name of the app for testing : myDomain/projectName)

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)
$routes->match(['get','post'], 'admin/translations/(: segment)''admin\Translations::translate\$1');  --> not working(404)
$routes->match(['get','post'], 'admin/translations/(: segment)''admin\Translations::translate/$1');  --> working 

PHP Code:
$routes->match(['get','post'], 'admin/translations/someVar''admin/Translations::translate/someVar'); --> working
->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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.