CakePHP routing - search parms - cakephp

I need to set up routing based on search parms (I'm using CakeDC search plugin)
URL should look like this: /apartments/studio-apartments
instead of this: /apartments/propertytype_id:1
I've already tried adding routes like this one:
Router::connect('/apartments/studio-apartments', array('controller'=>'apartments', 'action'=>'index'), array('pass'=>array('propertytype_id:1')));
or
Router::connect('/apartments/studio-apartments', array('controller' => 'apartments', 'action' => 'index', 'propertytype_id:1'));
or that one from Cake book

Try this in your routes.php:
Router::connect('/apartments/studio-apartments',
array('controller' => 'apartments','action' => 'index'),
array('propertytype_id' => '[0-9\-]+')
);

Related

How to redefine the URLs generation without changes at the HtmlHelper#link(...) calls in CakePHP?

I have a CakePHP website with many internal links, that are build with the HtmlHelper:
/app/View/MyController/myaction.ctp
<?php
echo $this->Html->link(
$item['Search']['name'],
array(
'controller' => 'targetcontroller',
'action' => 'targetaction',
$profileId,
$languageId
)
);
?>
It works fine with the default route:
/app/Config/routes.php
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
The generated links look like that: /mycontroller/myaction/$profileId/$languageId.
Now I want to use search engine friendly URLs (with profile names and ISO-639-1 language codes instead of IDs) for a part of the website and added a new Route:
/app/Config/routes.php
Router::connect(
'/:iso6391/:name.html',
array('controller' => 'mycontroller', 'action' => 'myaction'),
array(
'iso6391' => '[a-zA-Z]+',
'name' => '[0-9a-zA-ZäöüßÄÖÜ\-]+',
)
);
And it also works fine and the incomming URIs like /producer/en/TestName.html are interpreted correctly.
But the HtmlHelper is still generating the old URIs like /mycontroller/myaction/1/1.
The docu says:
Reverse routing is a feature in CakePHP that is used to allow you to easily change your URL structure without having to modify all your code. By using routing arrays to define your URLs, you can later configure routes and the generated URLs will automatically update.
Well, the HtmlHelper gets a routing array as input, that means: I'm using the reverse routing.
Why does it not work? How to make the HtmlHelper generate the new URLs (without changing the HtmlHelper#link(...) calls)?
Bit of explanation first
You are technically not using reverse routing. You see, the output link, /mycontroller/myaction/1/1 definitively doesn't match /iso/name.html. Like, in no way. So, the routing skips that rule because it doesn't apply.
Code
Try this
echo $this->Html->link(
$item['Search']['name'],
array(
'controller' => 'targetcontroller',
'action' => 'targetaction',
'iso6391' => $someStringWithIso,
'name' => $someName
)
);
But for that, you have to change your routing a bit, because you are not passing the parameters (check the docs for examples)
Router::connect(
'/:iso6391/:name.html',
array('controller' => 'mycontroller', 'action' => 'myaction'),
array(
'pass' => array('iso6391', 'name'),
'iso6391' => '[a-zA-Z]+',
'name' => '[0-9a-zA-ZäöüßÄÖÜ\-]+',
)
);
And you have to mind the first string match /:iso6391/:name.html. Do you want to match this route to every controller and action in your project, or just the one controller and the one view. If it is for all projects, just for precaution, use this
/:controller/:action/:iso6391/:name.html
if is just for, say, Controller1 and action "view", use
/controller1/view/:iso6391/:name.html
The detail you need to consider is the extension you use .html, is that really necessary in the url? If it is, add it as a parameter in the Html#link
echo $this->Html->link(
$item['Search']['name'],
array(
'controller' => 'targetcontroller',
'action' => 'targetaction',
'iso6391' => $someStringWithIso,
'name' => $someName
'ext' => 'html'
)
);
and also add parseExtensions to the routing file. Read this. Would be easier if you don't add the extension, but that's up to you.
In the end, you still have to change your calls to Html->link...

CakePHP routing to controller and page

Is it possible to identify a controller and pagename in the same url
Router::connect('/:controller/');
Router::connect('/:pagename/', array('controller' => 'home','action'=>'index'));
www.example.com/controller
so that the controller goes to the :controller/index
www.example.com/pagename
so that the page goes to home/index
Its really confusing what really you wants. If a url ends with :controller like www.example.com/posts its generally map index action. Now if the url ends with pagename, means action, like www.example.com/mypage, you can map that as-
Router::connect('/mypage', array('controller' => 'homes', 'action' => 'index'));
So when a user browse www.example.com/mypage, it will map to HomesController and index action.
Try following code for pagename:
Router::connect('/:slug', array('controller' => 'home', 'action' => 'index'));
UPDATE 1
If you want to treat www.example.com/user as same as www.example.com/users then you need to add following, because CakePHP controller name is plural as per CakePHP naming convention:
Router::connect('/user/:action/*', array('controller' => 'users'));
UPDATE 2
Router::connect(':slug') overwrite all of default routing. So you need to use custom routing class as like as :
App::uses('SlugRoute', 'Routing/Route');
Router::connect(
'/:slug',
array('controller' => 'home', 'action' => 'index'),
array('routeClass' => 'SlugRoute')
);

cakephp routing with language sub directory

I am generating a link (this is for when the language is set as "fre"):
$html->link('About', array('controller' => 'pages', 'action' => 'about', 'language'=> 'fre')) ;
I also have a sub directory for languages [eng|fre] as shown above this can be either languages:
I am routing like so:
Router::connect('/:language/:controller/:action/*',
array(),
array('language' => 'eng|fre'));
// this worked before the language subdomain
Router::connect('/about',array('controller'=>'pages','action'=>'about'));
the problem I have is, I want the urls to be:
/eng/about
/fre/about
but obviously they are coming out as:
/eng/pages/about
/fre/pages/about
I think hypothetically this should work
Router::connect('/:language/about',array(
'controller'=>'pages',
'action'=>'about',
'language' => 'eng|fre'
));
EDIT: If all 20 are pages you could try something like
Router::connect('/:language/:action',array(
'controller'=>'pages',
'action' => 'about|contact|something|else',
'language' => 'eng|fre'
));
This allows you to use any 3-character language code for any page:
Router::connect(
'/:language/:controller/:action/*',
array(),
array('language'=>'[a-z]{3}')
);

CakePHP 2.3 pagination named args

I create schema for my pagination:
Router::connect(__('news') .'/'. __('paginate') .'/:page', array(
'controller' => 'news',
'action' => 'index'), array(
'persist' => array(
'language')));
But 'named' params is still empty in Controller
My question is How to preapre router to work with pagination by links like this: news/paginate/4, news/paginate/5, etc...
I use Cake 2.3
You cannot used named params in routes.
Unrelated, you don't need the second argument true in calls to __() since 2.2. Check the API.

Routing configuration in cakephp

I am trying to implement routing in cakephp. I want the urls to mapped like this...
www.example.com/nodes/main -> www.example.com/main
www.example.com/nodes/about -> www.example.com/about
So for this I wrote in my config/routes.php file..
Router::connect('/:action', array('controller' => 'nodes'));
Now, I got the thing going but when I click on the links, the url in browser appears like
www.example.com/nodes/main
www.example.com/nodes/about
Is there some way where I can get the urls to appear the way they are routed?
Setting in .htaccess or httpd.conf would be easy - but I don't have access to that.
Regards
Vikram
This should work:
Router::connect('/main', array('controller' => 'nodes', 'action' => 'main'));
Router::connect('/about', array('controller' => 'nodes', 'action' => 'about'));
You may also do something more powerful, like this:
$actions = array('main','about');
foreach ($actions as $action){
Router::connect('/$action', array('controller' => 'nodes', 'action' => '$action'));
}
Basically if your links are created with Html helper, with the following format:
<?php echo $this->Html->link('your link', array('controller'=>'nodes', 'action'=>'main'));?>
Then the Cake will convert the links properly to www.example.com/main
But if your links are
<?php echo $this->Html->link('your link', '/nodes/main/');?>
they will point to www.example.com/nodes/main

Resources