CakePHP routing parameters - php

I'm building a web application and have been banging my head against a brick wall on a certain routing rule.
Here is the URL I am trying to achieve.
localhost/admin/location/123/pages/index
I am using the admin prefix which is working fine by uncommenting the following in app/config/core.php
Configure::write('Routing.prefixes', array('admin'));
'location/123' I want to essentially pass as parameters, so location with id of 123, 'pages' is the controller, 'edit' is the action.
There are other controllers that could replace 'pages' so this needs to be dynamic/wildcard. For example, a location could have pages, posts, users, etc.
Can anyone help me on how to write the Router::connect statement for this? Everything I try from the documentation doesn't seem to work.
Many thanks!
James

Router::connect(
'/admin/location/:location/:controller/:action/*',
array('admin' => 'true'),
array('location' => '[0-9]+')
);
If you go to /admin/location/123/pages/display/home, for example, it will go to PagesController, display action and home parameter.
In $this->request->params, this route will send admin = true and location = 123

Related

How to set custom Route with alias as controller

The CMS i'm developing using Cakephp 2.0 has two main Controllers:
Pages
Categories
I'm trying to set the route.php to have the following behavior:
If the user request a Page, the URL should be something like:
http://www.something.com/pages-alias/article-name/id/whatever
If the user address a Category, the URL should be something like:
http://www.something.com/categories-alias/category-name/id/whatever
Please notice that following categories and pages i've used "alias".
To clarify with an example, the URLs for a website of a local restaurant will be:
http://www.something.com/course/wild-boar/68/2013-07-18
Where "course" will substitute "page". And
http://www.something.com/menu/valentine-day/8/2014-01-30
Where "menu" will substitute "category".
The View should not be explicited in the URL nor the Routing rules.
Both the cases will have the view automatically choosen by the controller after some internal check (having subcategory, having only one page or more pages, and so on) so that will be overridden by the controller.
I've got some clues about the use of sort-of "alias" to build the routing rules but unfortunately the documentation was not clear enough to me about how to manage the object to create my own custom route.
So, can someone try to explain it with some example different from the ones available in the CakePhP 2.x documentation?
Thanks in advance to anyone that can be helpful.
For reference i'll paste here the links i've already read:
Routing - Cakephp 2.0 Documentation
Bakery Article from Frank (i suppose this is for the v1.3)
That is what you want probably:
Router::connect(
'/:category_alias/:category_name/:id/:whatever',
array('controller' => 'Article', 'action' => 'view'),
array('pass' => array('category_alias','category_name','id','whatever'),
'id' => '[0-9]+')
);
ofc you can delete this validator for id.. or add more validators :)
Then you can use function in ArticleController.php
public function view($category_alias, $category_name, $id, $whatever) {}

Cakephp Controller alias not working completely

I have a new question related to this question, but since the questions do not match at all, I am creating this new topic. But it is for good reference.
I changed my mind about the logs to log thing, logs is fine. But I am having another controller called WikisController, in this case I would really like to change the URL. I just hate it that the URL will be example.com/wikis/subject.html, I want it to be example.com/wiki/subject.html. It is much cleaner and the convention is now a 100% match with other sites where it is also called wiki, and not wikis. But, I want the controller to be called WikisController, because of the CakePHP convention.
Well, now comes the real problem. I was currently using the following routing in Config/routes.php:
Router::connect('/wiki/:action/*', array('controller' => 'wikis'));
Router::connect('/wiki/*', array('controller' => 'wikis'));
This is the piece of code used by the solution in the other topic. But now I am running into a little bit of trouble. Well, it isn't really a big issue because everything works just fine. But I don't like the URL.
When I create a link as the following from example.com/wiki/edit.html:
echo $this->Html->link('Wiki overview', array(
'controller' => 'wikis',
'action' => 'index',
'ext' => 'html'
));
It is creating a link to example.com/wiki/index.html totally fine and it works, but I don't want to show the index.html, I just don't. It is a word extra in the url and it is not necessary for the user to understand where he is.
I am creating the link with the controller and action key because of a little bug I was having earlier. When I didn't specify the action, it would create a link to example.com/edit.html which I don't want, obviously. So I have to add the action => index key to the array. I am not sure if this is a real bug, but it shouldn't matter. Index = index and nothing changes that. It is a good thing that I am sure my URL is pointing to the right page, so adding the action isn't an issue for me.
Just for good notice:
When I am creating a link on exactly the same way as I did above to example.com/flights/index.html from the page example.com/flights/add.html it would delete the /index part from the url and simply create a url to example.com/flights.html which is much cleaner.
So it has to do something with the routing, but I can't figure out what.
If I understood you correctly, then you'll need a route that connects /wiki to the controllers index action:
Router::connect('/wiki', array('controller' => 'wikis', 'action' => 'index'));
Router::connect('/wiki/:action/*', array('controller' => 'wikis'));
...

CakePHP - routing with wildcards (controller not found error)

I'm not really even sure how exactly to search for this but I have a URL
site.com/forum/controller/action
Where forum is a plugin and I currently have it routing to the plugin forum successfully with
Router::connect('/forum', array('plugin' => 'forum', 'controller' => 'home', 'action' => 'index'));
However, I want to add a route that will connect any top-level subdirectory to the plugin forum. For example,
site.com/fish/controller/action
site.com/bird/controller/action
would both route to the forum plugin. Similarly,
site.com/bird
would also route to the forum plugin. This is the best I have been able to come up with and it has no effect (I get a "FishController could not be found":
Router::connect('/*/:controller/:action/*', array('plugin' => 'forum'));
The closest answer I could find basically says this might not be possible? http://cakephp.1045679.n5.nabble.com/Routes-with-wildcards-missing-controller-errors-td1263632.html
EDIT: After some more trial & error I tried this:
Router::connect('/:site/:controller/:action/*', array('plugin' => 'forum'));
And it works. Could someone explain this?
The documentation at http://api.cakephp.org/class/router#method-Routerconnect does a great job at explaining this.
What you've done is created a custom parameter. Cake uses an array to keep track of the parameters and that how it know which controller, action and other parameters have been passed. The Router will convert any URLs with 3 slashes (/) to $param['site'], $param['controller'] and $param['action'].
From your controller, you can retrieve the value of :site by using $this->params['site'].

Cakephp Routing

I want that users can surf to http://www.yyy.com/xxx for xxx being a parameter. and so with www.yyy.com/xxx/zzz . I have following routing which works fine:
Router::connect('/:town', array('controller'=>'places', 'action'=>'index'), array('pass' => array('town')));
Router::connect('/:town/:category', array('controller'=>'places', 'action'=>'index'), array('pass' => array('town', 'category')));
But when I want tot surf to a different controller example www.yyy.com/differentcontroller/add it goes back to the places controller unless I make a routing for it...
any ideas?
The second rule on your routing list is simply looking for two sets of any combinations of characters after the domain name and treating the first as a town and the second as a category. As a result it mistakenly parses 'differentcontroller' as a town name and 'add' as a category. If you want to keep this URL structure then you'll need to add more specific routes to your routing file to cover situations like the 'add' route, or consider changing your existing URL layout to something more specific, like:
Router::connect('/places/:town', array('controller'=>'places', 'action'=>'index'), array('pass' => array('town')));
Router::connect('/places/:town/:category', array('controller'=>'places', 'action'=>'index'), array('pass' => array('town', 'category')));
My memories about cake routing is quite rusty, but you can use :controller/:action as a rule to make cake look for a valid controller/action pair, if I remember correctly.

cakephp routing for cushycms preview link

I have a set of rather static pages wich I moved to the views/pages folder. The resulting *.ctp files are editable by my customer through CushyCMS (simplistic cms perfect for dummy proof editing). However CushyCMS generated preview links that obviously don't take CakePHP into account. I would like to solve this little problem with custom routing, but can't get my head around the details..
How can I dynamically connect the url http://localhost:8888/cake125/app/views/pages/test.ctp to http://localhost:8888/cake125/pages/test?
I added the following in my routes.php:
Router::connect('/pages/test.ctp', array(
'controller' => 'pages',
'action' => 'display', 'test'));
This works ok for connecting: http://localhost:8888/cake125/pages/test.ctp to http://localhost:8888/cake125/pages/test. Somehow following snibbet doesn't do the trick:
Router::connect('/app/views/pages/test.ctp', array(
'controller' => 'pages',
'action' => 'display', 'test'));
Ideally I'd like to have a single Router::connect statement which connects all /app/views/pages/*.ctp requests to the right place.
Finally I would also like to correctly handle google search results for the old version of the site. Like so:
Router::connect('/test.html', array(
'controller' => 'pages',
'action' => 'display', 'test'));
This works ok but I'd rather have anypage.html connect to /pages/anypage. Can anyone help with this?
Thanks in advance!
First, by virtue of having Cake in a subdirectory (/cake125), I think you may need to connect the /cake125/:controller/:action, rather than how you have it. Not 100%, though; Cake might be robust enough to handle that use case. If you have weird errors, I'd check that.
On with my answer:
I think you are somewhat misunderstanding how the Router class works. You connect URLs, not relative filesystem paths, using Router::connect. By default (which you may have erased, but it's pretty simple to fix), Cake will route requests to /pages/* to the PagesController::display() function, passing it one argument (the action listed in the http request).
So, to have the pages controller map /pages/one to the app/views/pages/one.ctp element, simply make sure that the following (default, i.e. Cake normally has this setup) line is in the routes config (and make sure that lines above it do not match that pattern):
Router::connect( '/pages/:action', array( 'controller' => 'pages', 'action' => 'display', :action);
This should ensure that PagesController::display( $action ) is invoked during the request, which is (I think) what you're after.
If your CMS generates preview links that you want to correctly re-route, I'd suggest adding a new route. E.g., if your CMS generates links like http://somesite.com/cms/preview/newly_edited_file, you can route it like this:
Router::connect( '/cms/preview/:action', array( 'controller' => 'pages', 'action' => 'display', :action );
For your second question: have a default rule in your routes (make it the last rule, and have it match *). It will then be configured to route all not found requests to your controller/action pair as requested. Try this:
Router::connect( '/:action', array( 'controller' => 'pages', 'action' => 'display', :action );
Major caveat this will break your existing routes. You will need to manually add an entry for each of your existing controllers (Router::connect( '/users/:action', ...etc...). If you google around you can find some clever solutions, such as having that list generated at runtime for you. But you will need to address "normal" routing, once you've added that catch-all (and make sure your catch-all is at the end of the routing file).
Also, if you want to parse URLs like /test.html, simply add a call to Router::parseExtensions(...) so that Cake will register .html as an extension for it to parse. Check the manual on that function for more info.
As others have pointed out how CakePHP Router works, I'll leave it at that.
For the second part of your question (handling old links), I'd suggest adding this to the end of your Routes list:
Router::connect( '/:page',
array (
'controller' => 'pages',
'action' => 'display',
),
array (
'pass' => array ('page'), // to pass the page as first arg to action
'page' => '.+\.html$', // to verify that it ends with .html
)
);
You'd unfortunately have to parse out the .html yourself though
How can I dynamically connect the url http://localhost:8888/cake125/app/views/pages/test.ctp to http://localhost:8888/cake125/pages/test?
Well, the thing is, you don't. :-)
What I mean by that is, you do not connect a URL to another URL. What you really do is, you make certain URLs trigger certain Controller functions (or Actions for short) which in turn may (or may not) render certain Views. By default it's all straight forward through naming conventions. The URL /foo/bar triggers the Controller Foo's Action bar and renders the View /views/foo/bar.ctp.
The PagesController is already a special case. The URL /pages/foo triggers the Controller Pages's Action display, passes it the parameter foo, which renders the View /views/pages/foo.ctp. Notice the difference in which Action is triggered.
Since there are a lot of steps inbetween, it's not a given that a certain URL corresponds to a particular file on the hard disk. The URL /foo/bar might trigger Controller Baz' Action doh which renders the View /views/narf/glob.ctp.
This makes translating http://localhost:8888/cake125/app/views/pages/test.ctp to render the file /views/pages/test.ctp somewhere between an uncertainty and a pain in the rear.
Edit:
Having said that, the particular problem in your case is that the base URL is http://localhost:8888/cake125/app/. You can invoke a Cake app from http://localhost:8888/cake125/, http://localhost:8888/cake125/app/ or http://localhost:8888/cake125/app/webroot. All three URLs will be handled by the same file cake125/app/webroot/index.php, if you use one of the shorter URLs the request will be "forwarded" (rewritten) via .htaccess rules.
So the Route you're trying to connect, the Route that Cake sees, is actually /views/pages/test.ctp.
Actually, my mistake, this might not be the problem, but it depends on your .htaccess files and server configuration.
It doesn't seem to make much sense in a CMS though, since every newly created page would need its own rule. So I'd recommend against trying to do so and rather hack Cushy to properly construct URLs using the Cake HtmlHelper or Router::url(). Failing that, connect all URLs with a catch-all rule to some Action, parse the URL there and render the correct View "manually".
Alternatively, use .htaccess files and rewrite rules to actually rewrite the URL into a normal Cake URL, so Cake doesn't have to worry about it. As said above though, this can be very fragile.

Categories