Router::connect('/(?i:about)', array('controller' => 'pages', 'action'=>'display', 'about'));
used to work in CakePHP 1.2 but it seems to not be working in CakePHP 2.x
The only other solution that I see is to use the options to set a regular expression that will match against the a flag in the rout, but it seems rather convoluted for something so simple:
Router::connect('/:aboutFlag', array('controller' => 'pages', 'action'=>'display', 'about'),array("aboutFlag"=>"[aA]bout"));
Is there an easier solution more akin to how it was done in cake 1.2?
Router::connect('/:aboutFlag', array('controller' => 'pages', 'action'=>'display', 'about'),array("aboutFlag"=>"(?i:about)"));
This stems from the answer posted here
Related
Router::connect('/form/search*', array('controller' => 'Mycontoller', 'action' => 'search'));
I am really confused about this. I have query parameters in my URL for eg. localhost:8888/Search?srchTxt=Val.
I need to route everything from Search to a method in the controller. How can I do that in route.php file in CakePHP? is there another way I can accomplish it?
$routes->connect('/Search', ['controller' => 'Others', 'action' => 'my_action']);
https://book.cakephp.org/3.0/en/development/routing.html#routes-configuration
everything after question mark (?) is accessible thru $this->request->query()
https://book.cakephp.org/3.0/en/controllers/request-response.html#query-string-parameters
I've been to the darkest corners of the internet, searching for an answer to this.
It's such a simple question, but I just can't make a link to a subdomain using Cake's HTML helper. I know that in 2.0, you can do $this->html->link('Subdomain', array('subdomain' => 'hellosub', 'controller' => 'foo', 'action' => 'bar')); but I'm rolling with 1.3..
Thanks :-)
I'd like to force my site's URL to always have a language suffix.
So, if they type www.mysite.com it should take them to www.mysite.com/en.
I have a default language, so that should be used if it's their first time to the site. If it's not, I have a Cookie being set that I can use...but - I don't know where to use it.
I thought about checking to see if there was a "language" parameter in the URL, then if not, redirecting, but - that seems overkill - is there a better way? Can I do this in routes? or bootstrap?
The most efficient way would be through your web server. You can easily check if the request is for / (the home page) and redirect to /en.
Check the docs for what ever web server you are using, they all have something like mod_rewrite or similar.
Edit
You could set up a route like /set_default_language to redirect to in case of /, this controller can access the db and do what ever it needs.
Alternatively you can make it redirect to /your/usual/language_switch with no language specified and allow the code to use the default.
What I did:
I ended up checking in the AppController's beforeFilter() whether or not $this->request->params['langauge'] was set and if not, building the URL accordingly:
//Redirect to same url, but with language parameter
if (empty($this->request->params['language']) &&
empty($this->request->params['admin'])) {
$defaultLanguageCode = Configure::read('Languages.default.code2');
$cookiedLanguage = $this->Language->activeLanguageByCode($this->Cookie->read('lang'));
$languageToRedirectTo = (!empty($cookiedLanguage['code2'])) ? cookiedLanguage['code2'] : $defaultLanguageCode;
$newURL = '/' . $languageToRedirectTo . $this->request->here;
$this->redirect($newURL);
}
Note:
The part I couldn't figure out (until getting help in IRC) was to build the URL using $this->request->here, which is just the URL as a string. Prior to that I tried building out the array using the params array, but had no luck.
My routes (in case they help anyone)
(Keep in mind, I'm a routes noob, so - although they seem to be working for me, I do NOT guarantee they're done well!)
//root URL and root URL w/ language
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); // eg: www.google.com takes them to pages/display/home
Router::connect('/:language', array('controller'=>'pages', 'action' => 'display', 'home'), array('language'=>'[a-z]{2}')); // eg: /en takes them to pages/display/home and sets language
//pages (eg. /en/r/the_matrix or /r/the_matrix)
Router::connect('/:language/r/:slug/*', array('controller'=>'pages', 'action'=>'display'), array('language'=>'[a-z]{2}', 'pass'=>array('slug')));
Router::connect('/r/:slug/*', array('controller'=>'pages', 'action'=>'display'), array('pass'=>array('slug')));
//adds language to default URL
Router::connect('/:language/:controller/:action/*', array(), array('language'=>'[a-z]{2}'));
//Route prefixes
Configure::write('Routing.prefixes', array('admin'));
//User related
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/myaccount', array('controller' => 'users', 'action' => 'my_account'));
//
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';
If you don't want to redirect the requestAction calls from within your controllers or views you should add the following condition to your IF statement
if (empty($this->request->params['language']) &&
empty($this->request->params['admin']) &&
empty($this->request->params['requested'])) {
...
}
I have rewritten my site in Cakephp and choosen to keep the new Cakephp structure. I was wondering if I could use routing in Cakephp for 301-routing (permanently moved).
I want to redirect resources.php, languages.php, clips.php, possibly *.php, to /resources/, /languages/, /clips.
Can this type of 301 redirecting be easily done in CakePHP? I could even write a simple admin-interface to add 301-links, e.g. from a MySQL table to easily administer redirects. Or is it better to do this manually via mod_rewrite?
I'm not sure about the best way, but I would first put routing at routes php like:
Router::connect('/resources.php', array(
'controller' => 'resources',
'action' => 'index'
)
);
(and so on)
After that check at start of the action function which route was used, and if *.php route was used do a 301 redirect:
$this->redirect(array('controller' => 'resources', 'action' => 'index'), 301);
I guess there is also "smarter" way to implement this but this was the idea. (use of before_filter etc)
Since CakePhp 2.x there is Router::redirect() method.
So you could add redirection in your routs:
Router::redirect(
'/resources.php',
array(
'controller' => 'resources',
'action' => 'index'
),
array('status' => 301)
);
The third parameter array('status'=>301) is not necessary because 301-redirect is used by default.
See Redirect routing — CakePHP Cookbook v2.x documentation.
In my Cake application I have a controller "completed_projects". Its index action takes no arguments and lists some projects. Different pages can be accessed by
example.com/completed_projects/index/page:23 etc.
I want to make the url's like this:
example.com/portfolio/page23
Obviously I need to make some routes for this. I've tried many of them like:
Router::connect('/portfolio/page:num', array('controller' => 'completed_projects', 'action' => 'index'), array('pass'=>'page:num', 'num'=>'[0-9]+'));
and also:
Router::connect('/portfolio/:page:num', array('controller' => 'completed_projects', 'action' => 'index'), array('named'=>'num', 'page'=>'page', 'num'=>'[0-9]+'));
I also tried modifying them again and again but none of them works well.
I am using CakePHP 1.3. Any help will be appreciated.
Router::connect('/portfolio/page:page_num',
array('controller'=>'completed_projects', 'action'=>'index'),
array('page_num'=>'[\d]+')
);
In your controller, access page_num with:
$this->params['page_num'];