开发者

Codeigniter Dynamically load controller depending on url (outside routes.php)

using CodeIgniter normally one has to specify the controllers in the config/routes.php file. This is not to handy, so I would like to be able to do something like this in a controller.

  1. get url parts and check if the first part is specified in an array
  2. if so, load the specified controller, if not, load default controller.

It basically mimics the behavior of the routes file, but there is no need to specify the wildcards before. I am using a base controller I extend with every controller, but I would like to have this controller just load (or include) the needed controller.

Does anyone have any idea how I can do this in a good way?

Thanks in advance.

// Edit

Okay, so here is my scenario.

I have cms and users can choose to include modules (e.g. a gallery). I need to inlcude all the gallery php scripts without having to have "gallery" in the url. I figured 开发者_StackOverflow社区it would work if I use a "main controller" which loads another controller depending on the modules chosen. I realize this might not be the best way, so if there is a "clean" way to do it, please tell me.

As far as I know models are just for database stuff, so putting a whole gallery in there is not right either. The Plugin itself will of course be a library, but there is going to be some code to load the libs depending on the demands, get the database data, etc.

Thanks


The way you're doing this is incorrect. You never should take over the routing function to do this. What you need is to use some kind of module functionality to include the required methods and models; a module doesn't require to have route-accessible methods, so it's basically a "library" with a model and views.

If I remember correctly there are several plugins that provide this, One was HMVC (google it).

The ideal form would be to load the "Module" on demand from your controller, like you do with the core CodeIgniter Libraries; so lets say you're inside the blog controller action and you want to include the comment module which is used on gallery and on images, you just include the module and call it's methods to get the data which you can then pass to the view as needed; you can even render partials and store them into a variable to pass to your master controller.

Hope this is enough to get you on the right track :)


I may be misunderstanding your question but you want to load your controllers if you go to them and if not you want to go to your default.

If I am understanding correctly you can do a couple things in your routes have a route that takes everything to your default controller.

In your controller have an array of all your controllers then implode the array to a regular expression

$array = [c1, c2, c3, c4];
$str = implode('|', $array);
$regex = "($str)"

now just add your regex to a route

now redirect as you see fit.

But this is really what the routes file is for you you are just dancing around something that should be used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜