CodeIgniter Forums
better auto routing for controllers inside folders - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forum-10.html)
+--- Thread: better auto routing for controllers inside folders (/thread-79952.html)



better auto routing for controllers inside folders - ShinProg - 08-22-2021

Hello,
I'm creating a standalone image manager module for my website.
I have a controller class with 8 public methods (so, 8 pages that might share the same private helpers and constants), but even if there is currently one file, I want to organize every container modules into a folder :

src/
├───Controllers/
│  │  someFiles.php
│  └───Img/
│          Img.php


The URL to access to the "image foobar preview page" (which is the "view" method with the argument "foobar" inside the Controllers/Img/Img.php class) is localhost:8080/Img/Img/view/foobar.
The URL is a bit big, so I want to change it to "localhost:8080/img/view/foobar" easily.

What I tried so far and don't work :
  • Renaming Img.php to Home.php. While it works for the index page (/img/ -> Home::index), the auto-routing don't work with a method (/img/view/foobar will result to View::foobar() instead of Home::view('foobar')).
  • Still rename Img.php to Home.php but move the Home.php::view($image)method into View.php::index($image). Again, this works well for an url like /img/view/, but not when it comes to an argument (img/view/foobar will result into View.php::foobar() instead of  View.php::index("foobar"))

Is there a way to achieve what I want, without making a route rule for every method inside the controller ?

Here is the source code of my controller as of today (commit 2df0b2f) : https://github.com/Kagescan/code.kagescan.fr/blob/codeIgniter/App/Controllers/Img/Img.php

Thanks in advance !


RE: better auto routing for controllers inside folders - wuuyun - 08-22-2021

[quote pid="389482" dateline="1629662131"]
i think is difficult to solve .
suggest create your own core router class  
https://codeigniter.com/user_guide/extending/core_classes.html#extending-core-classes

[/quote]


RE: better auto routing for controllers inside folders - InsiteFX - 08-23-2021

This might be what your looking for.
CodeIgniter User Guide - Remapping Method Calls
Read it carerfully.


RE: better auto routing for controllers inside folders - ShinProg - 11-21-2021

Hmm that what I'm looking for, thanks you so much for the reply !