• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
routing / remapping / default_controller question

#1
[eluser]Mark75[/eluser]
Hi,

i use hmvc and have a routing/remapping question. I want to keep my global routes.php clean and let my module-controllers do all the logic using _remap.

This is in my global routes.php
Code:
$route['default_controller'] = "page";
$route['scaffolding_trigger'] = "";

My page controller uses _remap to handle all incoming requests. That works fine if
1) the URI contains no data (because it is the default_controller) or
2) i add
Code:
$route[':any'] = "page";
to routes.php, but then i have to make routes for all other controllers, "blog" for example.

Is it possible to route all requests to the default_controller without defining extra routes for my other controllers?

Regards
Mark

#2
[eluser]Phil Sturgeon[/eluser]
My CMS has been using routes set for each module with its own routes.php which didnt seem like too much of a pain until I saw this post.

Thinking about it, we only want requests with 1 url segment to go through to page controller right?

I haven't tested this, but in theory the following should get all 2+ segment URL's working.

Code:
$route['([a-z_-]+)/(.*)'] = "$1/$2";
$route['([a-z_-]+)'] = "page/index/$1";

$route['default_controller'] = "page";
$route['scaffolding_trigger'] = "";

I will make some modifications and test an evil plan to get all 1 segments working if they exist. Watch this space :-)

#3
[eluser]Mark75[/eluser]
Hi again,
to clearify: i want urls like

Code:
www.example.com
www.example.com/mypage
www.example.com/myotherpage
www.example.com/mypage/mysubpage
www.example.com/mythirdpage/mysubpage/mysubsubpage

default routed to my page-controller and

Code:
www.example.com/blog
www.example.com/blog/2008
www.example.com/blog/2008/12
www.example.com/blog/2008/12/test
or
Code:
www.example.com/portfolio
www.example.com/portfolio/web
www.example.com/portfolio/web/client

routed to their corresponding controllers, the same for any other controller i might add.

short: route everything to the pages controller if no other controller with the "segment-1-name" exists.

Mark

#4
[eluser]simshaun[/eluser]
I think you'd be best off writing a "Router" controller with a _remap.

Every request is passed to the Router, which determines if segment 1 is a controller or not.

#5
[eluser]melpuggi[/eluser]
[quote author="pyromaniac" date="1229727600"]
Code:
$route['([a-z_-]+)/(.*)'] = "$1/$2";
$route['([a-z_-]+)'] = "page/index/$1";

$route['default_controller'] = "page";
$route['scaffolding_trigger'] = "";

oh thank you Smile

#6
[eluser]Dyllon[/eluser]
You can extend the CI Router

application/libraries/My_Router.php
Code:
class MY_Router extends CI_Router {

    function MY_Router()
    {
        parent::CI_Router();
    }

    function _validate_request($segments)
    {
        // Does the requested controller NOT exist?
        if (!file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            $segments = array("page","some_function",$segments[0]);
            
        }
    }
    return parent::_validate_request($segments);
}

#7
[eluser]Phil Sturgeon[/eluser]
Yea I started doing it the same way as Dyllon after seeing this method used in another post.

It's much harder getting it working properly with Matchbox though! Undecided


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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