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

#1
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 !
Reply

#2
I think (: segment) shouldn't have that extra space. Like (Confusedegment) instead of (: segment)
Reply

#3
(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...
Reply

#4
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 )
Reply

#5
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 :
Code:
$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');
Reply

#6
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'); 
Reply

#7
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>
<p>admin</p>
</div>

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
$routes->match(['get','post'], 'admin/translations/myfield', 'admin/Translations::translate/myfield'); //works!!
strange... :huh:
But of course, I need to put arguments...
Reply

#8
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.
Reply

#9
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 ?
Reply

#10
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 

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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