I have the following route in my routes.php
Router::connect('/:lang/detail/:id/*', array('controller' => 'main', 'action' => 'detail'), array('lang' => '[a-z]{3}'));
and the following URL works
http://www.cyclistsroadmap.com/eng/detail/1380/Ferguson++119th/
But the following does not:
http://www.cyclistsroadmap.com/eng/detail/1380/Ferguson+%2f+119th/
(%2f is url encoded slash)
It would seem to me that greedy star should take anything but it doesn't seem to like the encoded slash in there. Is this something that I am doing wrong or is this a genuine bug in Cakephp?
Make sure you have enabled the AllowEncodedSlashes directive in Apache:
AllowEncodedSlashes on
More info can be found here:
http://httpd.apache.org/docs/2.2/mod/core.html#allowencodedslashes
Although this may not apply to your situation, I had a similar problem that I fixed in the following way.
We keep a URL for a tree like set of pages with it's path that get's updated on save. Ex:
Name | Path
Home | /
- Support | /support
-- Legal | /support/legal
-- Privacy | /support/privacy
- About | /about
-- Who We Are | /about/who-we-are
We then pass the path as an argument to our controller. The regular Router::* methods will encode the slashes in these. What we do instead is the following:
$redirect = explode('/', $path);
$redirect['controller'] = 'my_controller';
$redirect['action'] = 'my_action';
$this->redirect($redirect);
This let's cake re-encode the slashes for me. Then you can re-assemble them in the controller
$path = implode('/', $this->request->params['pass']);
Related
I add route like below
Router::connect('/:language/:controller/:action/*',
array(),
array('language' => '[a-z]{3}'));
I have two language src/Locale/jp and onother is src/Locale/fr
After add route configuration I have tried below URL
project/jp/tests/index
It's giving me error JpController not found.
How can I configure route for localization in cakephp 3.
Update :
In before filter I have added below code , but language is not changing
if($this->request->params['language']=='jp'){
Configure::write('Config.language','jp');
}
Have a close look at what you are passing, jp, that's two chars, now look at your regex, it requires exactly {3} chars - consequently, the route will not match.
On a side note, the folder name should be Locale, not local.
i would like to set base 'url' like "codeigniter" and that can be access in all views. I want to get full url like "http://localhost/somepath"
You probably don't need to do that, as the Helpers in Cake do that automatically.
For example, if your app is under http://localhost/somepath, creating a link like this
echo $this->Html->link('home', '/');
will automatically point to http://localhost/somepath
Links to actions work the same way:
echo $this->Html->link('login', ['controller' => 'Users', 'action' => 'login']);
will automatically point to http://localhost/somepath/Users/login
And if you do need to get the url anywhere else than in a view, you can do it like this:
use Cake\Routing\Router;
$path = Router::url('/', true);
In CakePHP 3 You can define "BASE_URL" constant in
yourAppFolder/config/paths.php as
define('BASE_URL', 'www.yoursite.com/');
and use BASE_URL anywhere in your project.
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 !
I am using cakePHP and got stuck in a situation. I want to pass a named parameter to my action that contains forward slashes. For example
http://www.test.com/claim/index/user:rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4=
Basically "rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4=" is one parameter but cake is only recognizing the first part of it until the fist slash.
When printing the named params array I get:
Array
(
[user] => rLbu78h2RpVwLTy
)
How can I escape the forward slash and allow cake to accept it as part of the named parameter?
Thank you
Don't use named-parameters
While you can use named parameters (/foo/this-is:a-named-parameter/), it's best to stick to normal url positional/path arguments and/or GET arguments. Over time it has proven not to be a great solution to pass around information using named parameters.
Use a trailing star route
If you modify the format of the url you're using to be e.g.:
/claim/index/<user token>
Then you can use a trailing star route to captuer everythiing that occurs after /index/ in a single variable:
Router::connect(
'/claim/index/**',
array('controller' => 'claims', 'action' => 'index')
);
In this way it doesn't matter what comes after /index/ you'll receive it in your index action:
// Request to /index/rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4=
function index($user) {
// $user === 'rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4='
}
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.