Welcome Guest, Not a member yet? Register   Sign In
How to I route a controller to a sub-folder with a wildcard?
#1
Exclamation 

I'm trying to figure out how to define routes for controllers nested in sub-folders but with a bit of a clause. I can get routes to work if I define each one individually but I want something that can simplify this if I have several controllers in subfolders that I want to access separately. Supposing I have a simplified controller folder structure like this:-
  • BaseController
  • Home
  • Process
  • SomeOtherController

And under 'Process' which is a folder, I create individual files for different classes, for example:-

Process ->
    -> MyFile1Class
    -> MyFile2Class
    -> MyFile3Class
    -> Etc...

I can create routes that work with something like this:-

PHP Code:
$routes->get('/''Home::index');
$routes->get('something''Something::index');
$routes->get('process/myfileclass1''Process\Myfileclass1::index');
$routes->get('process/myfileclass2''Process\Myfileclass2::index'); 

But what if I have lots of process files, each with an index method. How can I write the routes config file to be something like:-

$routes->get('process/(:any)', 'Process\{Method}::index');
or
$routes->get('process/(:any)', 'Process\$1::index');

Is there an easy way to do this that would allow classes to be added to the folder as required and the URI accessing those classes without having to define a route each time? I was able to do this easily in CI3 but I'm struggling in CI4.

Any assistance would be appreciated.
Reply
#2

You can't define such a route. CI4 does not allow it intentionally for security reasons.

But you can define many routes easily with loop like:
PHP Code:
$routes->get('process/myfileclass'.$i'Process\Myfileclass'.$i.'::index'); 
Reply
#3

(03-10-2024, 05:13 PM)kenjis Wrote: You can't define such a route. CI4 does not allow it intentionally for security reasons.

But you can define many routes easily with loop like:
PHP Code:
$routes->get('process/myfileclass'.$i'Process\Myfileclass'.$i.'::index'); 

Thanks kenjis. That method works if it's set for each class individually. Do you know what the security issues are with a more dynamic controller like CI3 had? I was able to control access in CI3 very easily so didn't see a problem.
Reply
#4

Since you are trying to do something that is prohibited by CI4, it is not surprising that it is not easy to do.

See https://github.com/codeigniter4/CodeIgni...ssues/2787

CI4 is not CI3. CI4 has much more features than CI3. And they may bring new security risks.
So if a feature in CI3 is no risk (I don't know it is really no risk), it does not mean the feature is secure in CI4.

Also we define routes so that we can keep track and/or know of them all.
You can see all routes with the `spark routes` command.
If you make a controller a wildcard, the actual routes are completely unknowable from the definition.
Then why bother defining routes at all? You could use Auto Routing.
Reply
#5

(03-11-2024, 04:22 AM)kenjis Wrote: Since you are trying to do something that is prohibited by CI4, it is not surprising that it is not easy to do.

See https://github.com/codeigniter4/CodeIgni...ssues/2787

CI4 is not CI3. CI4 has much more features than CI3. And they may bring new security risks.
So if a feature in CI3 is no risk (I don't know it is really no risk), it does not mean the feature is secure in CI4.

Also we define routes so that we can keep track and/or know of them all.
You can see all routes with the `spark routes` command.
If you make a controller a wildcard, the actual routes are completely unknowable from the definition.
Then why bother defining routes at all? You could use Auto Routing.

One of the reasons I worked with routes this way in CI3 was because it allowed me to add functionality in stages to very lean websites without padding out code that would increase bandwidth and processing power in other instances where methods weren’t required. So rather than having lots of methods under a single class and have the code read for everything each time, separating things in a logical order made certain websites with high traffic very lean. For each class, I would write a simple bit of code to restrict the URI and what was accessed anyway. Auto routing was open to more security risk I think so I never went down that road. That’s my explanation anyway! I think for now, I’ll just have to optimise my classes and declare the routes line by line.
Reply
#6

Yes, Auto Routing Legacy seems risky, but CI4 has improved Auto Routing.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB