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
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 have created a plugin called movies, I have used custom routes.
I have used pagination limit as '5'.
The first page is fine, but when I click on next or numbers. Those things doesn't works.
URL: something.com/movieslist/2
my Plugin/Movies/Config/routes.php
Router::connect('/movieslist', array('plugin' => 'Movies', 'controller' => 'Movies', 'action' => 'index'));
Router::connect('/movieslist/:page', array('plugin' => 'Movies', 'controller' => 'Movies', 'action' => 'index'));
my action code: Plugin/Movies/Controller/MoviesController.php
public function index() {
$this->Movie->recursive = 0;
$this->paginate = array('limit'=>5);
$this->set('movies', $this->paginate());
}
my view file code: Plugin/Movies/View/Movies/index.ctp
Same one from cakebake console. No changes made here.
Even the sort doesn't works :(
I'm tiered of searching my problems in many places :(
I had previously getting error in links itself and I fixed this after seeing this page:
CakePHP custom route pagination
Links are fixed but the links doesn't works. Pls don't down vote, I'm struggling from long time.
I'm using cakephp 2.0 version.
As a first step I'd swap the order of the router rules, since cakephp stops after the first match found. While the "/movieslist/:page" one will match a url with a page, it only does so for the named parameter page. Without it a url of the form "/movielist/2", might be interpreted as a link to "/movielist" with a normal parameter "2", hence the first router rule triggers.
If that does not work you can always just manually set the named page parameter. paginate just looks to see if it is set, but does not care if cakephp automagically figured it out from the url or if you do it yourself.
public function index($myPage=1) {
$this->Movie->recursive = 0;
$this->paginate = array('limit'=>5);
$this->params["page"] = $myPage;
$this->set('movies', $this->paginate());
}
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.
I need several routes in my application to allow for a dynamic string to proceed the prefix.
Here's my route:
Router::connect('/:location/traveler/:controller/*', array('action' => 'index', 'traveler' => true, 'prefix' => 'traveler'), array('pass' => array('location')));
For instance, if I went to /south/traveler/requests it would route successfully to RequestsController::traveler_index($location = 'south').
This is what I want, but I also need HtmlHelper::link() to properly reverse route a URL array into that route.
Here's my call to HtmlHelper::link():
$this->Html->link('List Requests', array('controller' => 'requests', 'action' => 'index', 'location' => 'south'));
The prefix routing is (or should be) implied since this is being called from a view within the traveler prefix.
The URL that call spits out is:
http://domain.com/traveler/requests/location:south
Have I not done something correctly? Is there any way I can avoid creating a custom route class to properly reverse route these URL arrays?
You need to inform the router that location should be a named parameter using Router::connectNamed. See the Named Parameters section of the CakePHP v1.3 Book:
URL: /contents/view/chapter:models/section:associations
When making custom routes, a common pitfall is that using named parameters will break your custom routes. In order to solve this you should inform the Router about which parameters are intended to be named parameters. Without this knowledge the Router is unable to determine whether named parameters are intended to actually be named parameters or routed parameters, and defaults to assuming you intended them to be routed parameters. To connect named parameters in the router use Router::connectNamed().
Router::connectNamed(array('chapter', 'section'));
Will ensure that your chapter and section parameters reverse route correctly.
I solved the problem.
Removing Router::connectNamed() from routes.php, I repaired my route which was misconfigured.
The reverse route to traveler_index() worked properly using the route I listed above, but any call to any other function, like traveler_edit() would fail.
Using the route below, I was able to get it to reverse route for any action on any controller in the traveler prefix with location as a variable.
Router::connect('/:location/traveler/:controller/:action/*', array('traveler' => true, 'prefix' => 'traveler'), array('pass' => array('location')));
Now, my call to HtmlHelper::link() correctly reverse-routes my URL array:
$this->Html->link('Edit Details', array('controller' => 'requests', 'action' => 'edit', 'location' => 'south', 1234));
...reverse routes to /south/traveler/requests/edit/1234.
I'm working on upgrading my project from CakePHP 1.2 to 1.3. In the process, it seems that the "magic" routing for plugins by which a controller name (e.g.: "ForumsController") matching the plugin name (e.g.: "forums") no longer automatically routes to the root of the plugin URL (e.g.: "www.example.com/forums" pointing to plugin "forums", controller "forums", action "index").
The error message given is as follows:
Error: ForumsController could not be found.
Error: Create the class ForumsController below in file: app/controllers/forums_controller.php
<?php
class ForumsController extends AppController {
var $name = 'Forums';
}
?>
In fact, even if I navigate to "www.example.com/forums/forums" or "www.example.com/forums/forums/index", I get the same exact error.
Do I need to explicitly set up routes to every single plugin I use? This seems to destroy a lot of the magic I like about CakePHP. I've only found that doing the following works:
Router::connect('/forums/:action/*', array('plugin' => 'forums', 'controller' => 'forums'));
Router::connect('/forums', array('plugin' => 'forums', 'controller' => 'forums', 'action' => 'index'));
Setting up 2 routes for every single plugin seems like overkill, does it not? Is there a better solution that will cover all my plugins, or at least reduce the number of routes I need to set up for each plugin?
I guess, that topic Configuration-and-application-bootstrapping covers that:
App::build(array(
'plugins' => array('/full/path/to/plugins/', '/next/full/path/to/plugins/')
));
Also take a look at this ticket: http://cakephp.lighthouseapp.com/projects/42648/tickets/750-plugin-route-problem-when-acl-and-auth-components-used#ticket-750-5 (Cake 1.3 had removed magic plugin routes).
You don't have myplugin_app_controller.php in your /app/plugins/myplugin directory.
Just create a file containing following:
<?php
class MypluginAppController extends AppController {
}
?>
And you will have all your plugin's features. :)