I'm experimenting with SEO friendly URL's in CakePHP as efficiently as I can, I've managed to use the current format, each example uses function view($slug) except for the first example which uses function index().
/categories/
/categories/books/
/categories/books/it-and-computing/
But what if IT & Computing has a sub-category "Web Development"? I'd like the URL to become:
/categories/books/it-and-computing/web-development/
I'm not sure how to do this without creating too many routes. Here is my route code so far:
Router::connect('/categories/', array('controller' => 'categories', 'action' => 'index'));
Router::connect('/categories/:slug',
array('controller' => 'categories', 'action' => 'view'),
array('pass' => array('slug'))
);
Router::connect('/categories/:parent/:slug',
array('controller' => 'categories', 'action' => 'view'),
array('pass' => array('parent', 'slug'))
);
Any help would be greatly appreciated
Kind Regards
Stephen
// in routes.php
Router::connect('/categories/:row:lastslash',array('controller' => 'settings', 'action' => 'show',),array(
'pass'=>array('row'),
'row'=>'.*?',
'lastslash'=>'\/?'
));
//in controller
function show($row = ""){
if($row){
$categories = split('/',$row);
?><pre><? print_r($categories);?></pre><?die();
}else{
die('do something else');
}
}
/categories/books/computing/web-development/cakephp/
result:
Array
(
[0] => books
[1] => computing
[2] => web-development
[3] => cakephp
)
/categories/
result:
do something else
/categories/books
result:
Array
(
[0] => books
)
Related
My subcategories and pages in categories have similar structure. For example:
site.com/cat/page
site.com/cat/subcat
How can I route them? URLs consist only of strings, no IDs and any numbers.
Pretty straightforward, your routes.php could look like:
$routes->connect(
'/cat/:page',
['controller' => 'Pages', 'action' => 'view'],
['page' => '\s+', 'pass' => ['page']]
);
$routes->connect(
'/cat/:subcat/:page',
['controller' => 'Pages', 'action' => 'subcatView'],
['subcat' => '\s+', 'page' => '\s+', 'pass' => ['subcat', 'page']]
);
Next, in your PagesController.php (actually you can use any other your controller/action, just substitute your names in routes above):
public function view($page) {
//i.e. if url is /cat/about then $page === 'about'
}
public function subcatView($subcat, $page) {
// i.e. if url is /cat/subcat/some_page then $subcat === 'subcat' & $page === 'some_page'
}
I highly recommend to refer cakephp3 documentation, routes section, it's clear enough to help you implement any magic.
CakePHP Version 2.5.2
Config.Language 'spa'
If i'm in
mydomain.com/backoffice
and i reload (ctrl-r) i get again
mydomain.com/backoffice
If i call in UsersController in action deleteCache at the end:
return $this->redirect('/backoffice');
i get
mydomain.com/users/sbackoffice
Please note the "s" before backoffice. That seems to be a part of the config.language
If i set the Config.language to 'eng' i get
mydomain.com/users/ebackoffice
Please note now the 'e' before backoffice.
My routes.php:
Router::connect('/backoffice', array('controller' => 'users', 'action' => 'backoffice'));
Router::connect('/:language/:controller/:action/*',
array(),
array('language' => 'eng|spa')
);
Router::connect('/:language/:controller',
array('action' => 'index'),
array('language' => 'eng|spa')
);
Router::connect('/:language', array('controller' => 'technologies', 'action' => 'home'),
array('language' => 'eng|spa')
);
Any idea?
How can i debug this?
Hi I d'like to be able to rewrite my url. I have this format of url:
localhost/example/urlrewriting/Links/view/1
and I want to change this url to
http://localhost/example/urlrewriting/tt/my-link-1
In my Routes.php I have done this:
Router::connect('tt/:slug-:id',
array('controller' =>'links', 'action' => 'test'),
array('pass' => array('slug', 'id'), 'slug' => '[a-z0-9\-]+', 'id' => '[0-9]+'));
and in my controller I have this
public function test($param1, $param2){
debug($this->request->params);
}
I've always find this answer:
TtController could not be found.
cakephp 2.4.1
Please add / before the connect params like:-
Router::connect('/tt/:slug-:id',
array('controller' =>'links', 'action' => 'test'),
array('pass' => array('slug', 'id'), 'slug' => '[a-z0-9\-]+', 'id' => '[0-9]+'));
What im trying to do here is maintain the variable 42 all throughout all pagination urls. I want my url to change from this
/exams/take/42/page:2
to this
/exams/take/42/items/2
Again,the number 42 is the variable..and the number 2 is the page number..Thanks.
UPDATE :
routes.php
Router::connect('/examinations/take/:id/page/:page',
array('controller' => 'examinations', 'action' => 'take'),
array(
'pass' => array('id', 'page'),
'id' => '[0-9]+',
'page' => '[0-9]+'
)
);
in the view/take
$this->Paginator->options(array('url' => $this->passedArgs));
AppController.php
public function beforeFilter(){
if (isset($this->request->params['page'])) {
$this->request->params['named']['page'] = $this->request->params['page'];
}
}
ive tried this ..but the generated url is the same,/examinations/take/42/page:2 ,when i click the next and prev links..
You've to define custom routes:
http://book.cakephp.org/2.0/en/development/routing.html
Example as below:
Router::connect(
'/exams/take/:id/items/:number',
array('controller' => 'exams', 'action' => 'take'),
array('pass' => array('id', 'number'))
);
Also you can get more information from below url try this:
http://www.sakic.net/blog/changing-cakephp-pagination-urls
yes you can do it with the use of custom routing. for more undrestanding you can see cakephp manual to manage custom routing in pagination
http://book.cakephp.org/2.0/en/development/routing.html
below will be your
e.g.
And by adding this route:
Router::connect('/:id/page/:page',
array('controller' => 'examinations', 'action' => 'take'),
array(
'pass' => array('id', 'page'),
'id' => '[0-9]+',
'page' => '[0-9]+'
)
);
and i does not work you can refer link
My localization files (.po) work if I change the default language, but I can't make the routes working, here's what I've got atm:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/login/*', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout/*', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/register/*', array('controller' => 'users', 'action' => 'register'));
Router::connect('/:lang/:controller/:action/*', array('lang' => 'en'), array('lang' => 'en|fr'));
But when I try : domain.com/fr/login, the cake is looking for the "fr" controller.
I am using this function in the AppController beforeFilter to switch between languages:
function setLanguage() {
if(!isset($this->params['lang']))
{
$this->params['lang'] = 'en';
}
$lang = $this->params['lang'];
App::import('Core', 'i18n');
$I18n =& I18n::getInstance();
$I18n->l10n->get($lang);
foreach (Configure::read('Config.languages') as $lang => $locale)
{
if($lang == $this->params['lang'])
{
$this->params['locale'] = $locale['locale'];
}
}
}
Cheers,
Nicolas.
You do not have a login controller. So your bottom route does not match and Cake then tries the default by looking for an fr controller.
Routes don't interact as you expect them to:
/login - would match your second route
/fr/users/login - would match your last route.
/fr/login - does NOT intelligently "merge" the two routes. You need to explicitly make such a route.