Configuring CakePHP 2.0 routing for domain/parameter (without controller name) - cakephp

I would like to be able to route something like the following in CakePHP 2.0:
domain.com/london
domain.com/milton keynes
to a specific controller and action.
The application has multiple controllers so it should only use this route if the parameter provided doesn't match a controller name.
I achieved this with CakePHP 1.3.12 by adding the following code to the bottom of config/routes.php
Router::connect(
'/:location',
array('controller' => 'articles', 'action' => 'testing'),
array('pass' => array('location'), 'location' => '[a-z ]+')
);
Using this code with CakePHP 2.0 only works if I comment out the require line from config/routes.php, but then I loose the default routes so that a URL pointing at any other controller is caught by this.
How can I achieve the desired routing?

As far as I know this shouldn't work in Cake 1.3 either, simply because your [a-z ]+ regex also matches the case for a simple /controller_name route; the Router has no way to distinguish between the two and will thus always route to the one it encounters first.
You can, however, create a custom route class to achieve this. Mark Story (one of the Cake devs) wrote an excellent post about it quite some time ago, it's for Cake 1.3 but you can easily apply the principle to 2.0 (I know 'cause I'm using it in my 2.0 app). You can the find the post here.

This might not answer the specific question asked above, but it's relevant, this page comes up in search results, and my goal is to save whoever finds it (including future me I guess) some time on not having to research what I just researched.
I needed to add routing with a parameter for a different domain. For example, example.com should behave as usual, while example.org/some_page should route directly to a specific controller and action. So I've added the following to my Config/routes.php:
if ( CakeRequest::host()=='example.org' ) {
Router::connect('/:my_variable',
array(
'controller'=>'my_controller',
'action'=>'my_action'
),
array(
'my_variable'=>'[a-zA-Z-0-9 ]+',
'pass'=>array('my_variable')
)
);
}

Related

CakePHP Routing & HTML Helper Link Ambiguity

I have a site that uses CakePHP 2.x. There's a backend interface where actions use the standard Cake layouts and views, but several of the actions are also exposed to front end users as "dialogs" (same functionality, just a layout that can be put in iframe).
In app/Config/router.php I have added the following:
Router::connect('/dialog/:controller', array('action' => 'index'));
Router::connect('/dialog/:controller/:action');
Router::connect('/dialog/:controller/:action/**');
This works appropriately, but the problem starts when trying to use the HTML helper's link() method. If I try to create a link like:
$this->Html->link('edit account', array('controller' => 'users', 'action' => 'edit'));
I get the following:
edit account
When the link is within a dialog, this works great, but I don't want the non-dialog pages to link to the dialog.
How can I control which of the two URLs is used in a particular page?
Is there something I can call from within AppController once I know whether the page being rendered is a dialog or not, or even something in the call to link() that would allow me to override it.
I know there's the "prefix" option which would allow for URLs like /user/dialog_edit but I would like to maintain the /dialog/users/edit format if possible. I also know I can hard code the URL vs. passing controller/action/id/etc in an array, and I don't anticipate pathing/model names changing, but I'd like to do this the idiomatic way for CakePHP, if possible.

How to route user generated paths in cakephp eg mysite.com/username1?

I'm making a cakephp 2.3 app and have these "pages":
mysite.com/demo
mysite.com/admin
The above are fixed. The below are generated:
mysite.com/johnsmith
mysite.com/cooluser88
I've updated the routes with:
Router::connect(
'/*/',
array('controller' => 'pages', 'action' => 'redirect')
);
Am I on the right track? a redirect method in the pages controller? I'm using the official docs but I haven't been able to find specific examples.
I'll keep working on it and will update with an answer if I find one!
Your best bet IMO is to do something like reddit where each slug is prefixed with /r/.
Example:
mysite.com/u/johnsmith
Then, in your routes, you can do something like this:
Router::connect('/u/:uname/*', array('controller'=>'users', 'action'=>'display'),
array('pass'=>array('uname')));
If you really don't want to do that, you can use something like:
Router::connect('/:uname/*', array('controller'=>'users', 'action'=>'display'),
array('pass'=>array('uname')));
But keep in mind, any other controllers, plugins...etc etc etc will need their own routes, since it will think that ANYTHING passed after the mysite.com/ is a uname. Not the best idea IMO.

dynamic html redirects using cakephp routers

dynamic html redirects  using routers
in the beginning I had no categories and all of my pages were root
example:
http://domain/somepage
This was great, but as my content over the years grew I need to categorize my content
so I added some routes see below
//routes.php
Router::connect(
"/:category/:slug",
array('controller' => 'controllername', 'action' => 'view'),
array(
'name'=>'[-A-Z0-9]+',
'pass' => array('category','slug')
)
);
//end
this works great and accomplished what I needed to do, but there is one problem the search engines .I need to write 301's for all of my links and I have over 8K pages.
The solution cakesphp's Router::redirect
The issue I am now having is I cant figurer out how to redirect my old links. I can for example redirect all of the links to one category, but that wont cut it. I need to redirect all of my links to the new location.
I am trying to use routes.php router :: redirect
if I do this my code it redirects to the category, but not the slug
Router::redirect(
'/:slug/*',
array(
'pass' => array('category/:slug'))
result
http://domain/category/
how can I get cake to redirect to
http://domain/category/slug ?
instead of http://domain/category/
I had all of my links pointing to the root directory
http://domain/somepage
http://domain/anotherpage
http://domain/ect
I needed to add categories
such as
`
http://domain.com/phones/samsung.php
http://domain.com/books/cakephp.php
`
I didn’t want to use .htaccees file because
My hosing provide limits me to 100 redirects
and i have over 8K links i need to redirect
I am trying to use cakes router ::redirect function in the routes.php file.
the below code works only for one category it doesn’t do it dynamically like I would like it too.
I tried to create a router class that would do this for me like you suggested, but to be honest with you I am not an expert in cakephp. Its easy to learn and a great framework I just dont know how to make my own classes or components yet. I just haven’t found good documentation to do this yet.
//routes.php
$move='category/'. stripslashes_deep ($_SERVER['REQUEST_URI']); Router::redirect('/:slug/*',$move, array('status' => 302)); code
Your best bet is to use a custom route class.
Custom routing extends the CakeRoute class and will allow you to return/add/modify parameters that are passed over.
CakePHP Custom Route Classes - How to Pass Arguments?

Route to absolute URL CakePHP 2.0

I want to access my sitemap.xml file in /mywebsite/sitemaps/xml/sitemap.xml
Reading some documentation, it is explained that I can reach the absolute URL by using:
Router::url('/',true)
For some reason it doesn't work. How to solve this?
If your site map xml is generated outside of CakePHP, then you can create the folders and file in /mywebsite/app/webroot/sitemaps/xml/sitemap.xml and avoid routing altogether.
If you are dynamically generating the xml through cake, then you don't want to hard-code the route. Rather, you would route to the controller and action that provide the xml return. A very basic example would be..
Router::connect(
'/sitemaps/xml/sitemap.xml',
array(
'controller' => 'Xml',
'action' => 'sitemap'
)
);
The XML documentation here can help you get started building and routing the site map.
http://book.cakephp.org/2.0/en/core-utility-libraries/xml.html

Use of Routing in CakePHP

Why we are using routing in cakePHP and what would be the basic approaches for implementation...?
Routing allows aliasing and routing(!) of URLs. It gives us a cleaner, more controlled interface and underpins the functioning of CakePHP.
The first step would be to read the appropriate chapter in the book: http://book.cakephp.org/view/542/Defining-Routes (1.2) or http://book.cakephp.org/view/948/Defining-Routes (1.3)
Then look at the routes.php file (app/config/routes.php) to understand how it goes together.
Finally, when you know what you want to do (we don't because you haven't told us), try it debug it and use it.
Why
Because it allows you to decouple your URLs from your controller actions. You can name your controllers and actions in a way that makes sense internally, and invoke them using URLs that do not need to bear any resemblance to your internal naming scheme.
FooApiVersion1Controller::internal_beta_method() can be invoked by the URL /api/v1/method, and you can swap out the controller or method at any time without needing to change the URL.
How
Read the manual. http://book.cakephp.org/view/945/Routes-Configuration
http://book.cakephp.org/view/310/Configuration
<?php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
?>

Resources