Cakephp 3 : Localization Routing - cakephp

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.

Related

Cakephp 3 problems accesing non plugin links from plugin pages

I have a cakephp app and I created a plugin based on cakephp 3.8 official documentation. Everything is good, I can access links like:
project.local/plugin/plugin-tests/
. The problem is that after I access that plugin link, all my links are updated with the plugin name. Eg: project.local/users/ is transformed into project.local/plugin/users/.
The values for plugin, prefix, controller and action are being persisted by default, meaning that if you don't specify them explicitly in your URL arrays, they inherit the value of the current context.
If you want your links to always point to a non-plugin target, make sure to set null for it, likewise set false for the prefix (not null), ie:
[
'plugin' => null, // break out of plugin contexts
'prefix' => false, // break out of prefix contexts
'controller' => 'Users',
'action' => 'index',
]
See also
Cookbook > Routing > Creating Links to Plugin Routes
Cookbook > Routing > Prefix Routing

CakePHP allow forward slash as named parameter value

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='
}

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.

Encoded slashes break route in Cakephp

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']);

Full view caching for dynamic routes in cakephp

I am having some issues with full view caching in cakephp.
The url that I would like to cache is /posts/badge/23/size:180x150 I have been able to cache the view successfully by adding $cacheAction = "1 hour"; to the controller.
Since I do not want to cache all methods in the controller (just specific methods) I tried to use the array syntax to match the url /posts/badge/23/size:180x150
where 23 is the post ID and size is a named parameter (there are 2
variations of size).
So my question is what is the proper (if any) match to place in
cacheAction to cache all posts/badges/* Here are some examples that I
have tried:
var $cacheAction = array(
'badge/*' => '+1 hour',
'posts/badge/23/size:180x150' => '1 hour',
'badge/23/size:180x150' => '1 hour',
'posts/badge/:id/:size' => '1 hour',
'badge/:id/:size' => '1 hour',
);
None of these seem to match (or at least do not cache for some
reason). I do need to cache posts/badge//size: and trap each of the
parameters being passed.
Again, if I set $cacheAction = "1 hour"; then the cache file is
created (the file tmp/cache/views/posts_badge_23_size_180x150.php is
created)
Has anyone been able to create a cache for a dynamic url?
CakePHP's CacheHelper currently isn't capable of Routes. CacheHelper::cache(), which is responsible for parsing and checking the $cacheAction settings does not use any Router functionality, it just does some string checking.
You can use your own CacheHelper, just modify the cache() method appropriately and place the file cache.php in app/views/helpers.
This ticket is similiar to your problem, the solution posted there may help you: trac.cakephp.org/ticket/6192
On this page: Caching in the Controller it says:
Remember to use your routes in the $cacheAction if you have any.
So try creating some routes that match your URL structure and using them for cacheAction keys. Please report back if it works.

Resources