cakephp Fix Url redirection - cakephp

OLD ---> NEW
http://abc.com/cities/view/1 -->http://abc.com/melbourne-day-tours/
http://abc.com/categories/view/3 -->http://abc.com/melbourne-day-tours/great-ocean-road-tours
http://abc.com/tours/view/1 -->http://abc.com/melbourne-day-tours/great-ocean-road-tours/great-ocean-road-adventure-tour
i do have an old project , now i have to do following redirection , what should i do ?
using htaccess ? or any other ways ?
when i put http://abc.com/melbourne-day-tours/ in my bro, the url in the address bar shd be stay same, and it be call controller and the action for http://abc.com/cities/view/1
thanks

The best way of of doing this is using the CakePHP Router. You can set routes in app/Config/routes.php, for example:
Router::connect(
'/melbourne-day-tours',
array('controller' => 'cities', 'action' => 'view', 1)
);
If you are planning having many routes you might be better of using slugs which are stored in your database. There is some information about that in the link above.

What i just done is :
i just done is just putted lines in app/webroot/.htaccess
RewriteRule ^melbourne-day-tours/?$ /cities/view/1 [L]
it worked !
thanks !

Related

How to get base url inside controller in CakePHP

I am using CakePHP 2.6.7. I want to get the following url:
www.mydomain.com/OrderFromReseller/82BC1562-22F9-4326-8B4B-370129710E8C
I try as:
$resellerURL = $this->webroot . 'OrderFromReseller/' . $this->request->data['Reseller']['api_key'];
But when I echo the value of $resellerURL it echos http://orderfromreseller/82BC1562-22F9-4326-8B4B-370129710E8C.
Is there any alternative of $this->webroot to get base url properly inside cakephp?
You want to use Router::url() to get the full URL. For example to get the full web address for the homepage:-
Router::url('/', true);
You can pass any router array as the first parameter to get the full URL as long as you pass true as the second parameter. For example:-
$url = Router::url(
['controller' => 'pages', 'action' => 'display', 'test'],
true
);
It work for me in cakephp 3.5 please follow the below code
1) add this code in the top of the controller below namespace App\Controller;
use Cake\Routing\Router;
2) $baseUrl = Router::url('/', true);
This is the best and simple way to get the base url in cakephp 3+

Cakephp change url?

i want change my url in cake php /eng/users/ to /users/; i mean when i entered this /eng/users/ cakephp don't show me error like this
*Missing Controller
Error: EngController could not be found.
Error: Create the class "EngController" below in file: "app\Controller\EngController.php" *
both this /eng/users/ and /users/ become same ? any solution?
You are looking for Router.
Read this article: http://book.cakephp.org/2.0/en/development/routing.html#routes-configuration
and especially for the cause of users ur you should add in your /app/Config/routes.php
Router::connect(
'/eng/:controller/*', array('controller' => 'users')
);
the eng can be set to some prefix and so on.

Forcing language prefix on URL in CakePHP

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'])) {
...
}

What's the best way to 301 redirect pages in CakePHP?

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.

CakePHP 1.1 "parseExtension" inexsistent

I have a problem with a site in CakePHP 1.1 which is impossible to migrate from version due to the size of the project.
I need to create methods to which call using extensions, for example:
$Route->connect('/xxx.xml', array ('controller' => 'Interactive', 'action' => 'xxx'));
But this does not work and the problem is that Cake 1.1 does not have this function:
Router::parseExtensions('xml');
Has anyone working with cakephp 1.1 had this problem? If so do they know how to fix it?
Thanks.
I'm not 100% familiar of the capabilities of CakePHP 1.1, but have you considered setting up a router to look for something like the following:
/:controller/:action.xml
Then you could control what controller/action it leads to and alter the layout.
You might need to escape the . on .xml.
I would seriously consider upgrading to CakePHP 1.3 and reading the migration docs.
The problem is not the layout, is this:
if i set this router:
$Route->connect('/xxx.xml', array ('controller' => 'Interactive', 'action' => 'xxx'));
or your example:
$Route->connect('/:controller/:xxx.xml', array ('controller' => 'Interactive', 'action' => 'xxx'));
the function called is:
class MuControlController extends AppController {
function xxx.xml() {
}
obiusly this function cant exists.

Resources