Catching urls with a controller

Permalink
Hi all,

Is it possible to 'catch' urls with a controller?

Lets say I have this url:

/something/somethingmore/

But there is no page called somethingmore. It's a page I want to create dynamically. So I would like a controller to be called when the url starts with /something/ and then build the page based on the remaining part of the url. How would I do that?

Ronald72
 
jordanlev replied on at Permalink Reply
jordanlev
I *think* this is only possible if the top-level /something/ page is a single_page. If that's the case, then what you do is add a new public function called "somethingmore()" to that single_page controller (in YOURSITE/controllers/something.php) and it should respond to that url. Note that unfortunately C5 doesn't allow you to have more than one view file for a single controller, so if you want the somethingmore page to look different than the something page, you need to put a big IF statement in the view (in YOURSITE/single_pages/something.php).
Ronald72 replied on at Permalink Reply
Ronald72
That could work but the problem is that a don't know the real string 'somethingmore'. It will be the name (slug) of a page, and those can also be added later. So unfortunately I can't create a method with that name. I need a method that will run whatever the 'somethingmore' is.
olliephillips replied on at Permalink Reply
olliephillips
This is possible with Jordan's approach.

Anything else in the URL gets passed to the controller's method as a parameter. So if you have your singlepage, with a default method you can handle a dynamic/unknown page in that method and output what you need dependent on what is passed. You just need to decide on a naming convention for the single page controller and method.

http://site/singlepagecontroller/method/whatever/you/want...

Hope that helps
Mnkras replied on at Permalink Reply
Mnkras
I believe you could do this using events,

$req = Request::get();
$path = $req->getRequestPath(); //this is the path requested

Then do checks to see if it is an invalid page, if so
$v = View::getInstance();
$v->render('/path/to/singlepage', array('path'=>$path)); //passes the $path variable


try doing that in an on_start method
jshannon replied on at Permalink Reply
jshannon
Ollie is right, but make sure you allow for the appropriate number of arguments in your "catch-all" method.

If you're expecting a url like /something/somethingmore/arg1/arg2, you'd want a method like public function view($page, $arg = null, $arg2 = null) {}. Otherwise c5 says "i have three arguments and no method will accept those, must be a 404".