cakephp 1.3.11 plugin routing not working - cakephp

I'm building a blog plugin for CakePHP. It is called 'blog'.
I am following the manual on plugin routing but my links won't work. http://book.cakephp.org/view/951/Plugin-routing
For example, when I am on this page: appname/blog/posts/index, I have a link to the index action of the users plugin. So I built my link as follows:
echo $this->Html->link(
__('List Users', true),
array(
'plugin' => 'users',
'controller' => 'users',
'action' => 'index'
)
);
But the link keep pointing to app/blog/users/index instead of app/users/users/index. Why is that?
PS: users is also a plugin (from CakeDC).

you can set routes like this:
Router::connect('/users', array('admin' => false, 'plugin' => false, 'controller' => 'tests', 'action' => 'test') );
'admin' is for admin routing and 'plugin' for plugin url.

Related

CakePHP-3.5.4 - Reverse routing not working as expected

In my routes.php file I define:
Router::prefix('admin', function ($routes) {
$routes->connect(
'/',
[
'plugin' => false,
'controller' => 'Users',
'action' => 'dashboard'
],
['_name' => 'admin_dashboard']
);
$routes->fallbacks(DashedRoute::class);
});
In controller, i do:
debug(Router::url(['_name' => 'admin_dashboard']));
It prints /admin which is correct.
But if, in the same controller, i do:
debug(Router::url([
'prefix' => 'admin',
'plugin' => false,
'controller' => 'Users',
'action' => 'dashboard'
]));
It prints /admin/users/dashboard which is incorrect, at least i think as i expect it to print /admin.
Is there something I am missing out in order to make reverse routing work?
I am using CakePHP 3.5.4
The plugin key doesn't really support booleans, the value should be either a string with the name of the plugin, or null to indicate that no plugin should be used.
Using false will cause the route to not be matched, and instead it's being catched by the fallback routes, hence you end up with the /admin/users/dashboard URL.
See also
Cookbook > Routing > Creating Links to Plugin Routes

CakePhp 3 link with parameter and extension

I need create a link with a parameter and .pdf extension, however nothing from older versions of CakePHP seem to work. Ideally it would be a postlink, but html link would be good as well.
Here is where I got so far.
//Html link
$this->Html->link('PDF', [ 'controller' => 'users', 'action' => 'pdf', $user['id'], 'ext' => 'pdf' ]);
//Form postLink
$this->Form->postLink('PDF', [ 'controller' => 'users', 'action' => 'pdf', $user['id'], 'ext' => 'pdf' ]);
The Html link results in: .../users/pdf/2?ext=pdf which then is not routed to the pdf template.
Check this...
http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#router
Most probably you need to add it as _ext

Cakephp Remove plugin and controller name from url for any action

I want to remove deal/deals for all the actions of deals controller:
http://example.com/deal/deals/voucher/dsfsdfdf should be http://example.com/voucher/cPH5aGr1
etc.
You should define you routes in you app/config/routes.php
Router::connect('/voucher/*', array('controller' => 'Deals', 'action' => 'voucher', 'plugin' => 'Deal'));

Hide controller name when calling its methods

I'm creating a website using CakePHP.
It has user Registration and Login system.
So at first i have a controller called HomeController which has three methods
1) index();
2) login();
3) register();
by default index() method will execute.
i have the following code to call other two methods.
<? php
echo $this->Html->link('Login',array('controller'=>'Home','action'=>'login'),array('escape'=>FALSE));
echo $this->Html->link('Register',array('controller'=>'Home','action'=>'register'),array('escape'=>FALSE));
?>
So now when i click on the above links (login,register) it will call appropriate method and the url will be something like
www.example.com/home/login and www.example.com/home/register
Now i want to remove the controller name from the url since the method is in the same controller.
So the url should look like
www.example.com/login and www.example.com/register
is it possible??
Please help..
in app\Config\routes.php add following lines
Router::connect('/login', array('controller' => 'home', 'action' => 'login'));
Router::connect('/register', array('controller' => 'home', 'action' => 'register'));
It will convert default urls to your desired urls
www.example.com/login and www.example.com/register
Router::parseExtensions('json');
Router::connect('/*', array('controller' => 'home',
'action' => 'login'));
Router::connect('/login', array('controller' => 'home',
'action' => 'login'));

Url Rewriting of once controller - cake 2.0

I'm developing a simple CMS in CakePHP, right now it has 4 controllers in it(Menus,Site,Roles,Users), I want to rewrite one controller, but I'm having problem.
I use all the actions only as admins for admin purpose like admin_view, admin_add......
except siteController(this controller is only for frontend purpose)
I need my www.example.com/site/view/something_here must be replaced to www.example.com/something_here - this will be displayed in front-end so.
I added a line in my routes file:
Router::connect('/*', array('controller' => 'site', 'action' => 'view'));
But after adding this I couldn't able to use other controllers.
I again added some more lines before the above line:
Router::connect('/admin/Menus/*', array('controller' => 'menus', 'prefix' => 'admin'));
Same for all other controllers, but if I send any action or id in url it doesn't works.
like - http://www.exmple.com/admin/menus/[view/1] - the one inside square bracket doesn't works.
any Ideas on rewriting this?
I just answered a similar question on another thread.
To put the admin controller routes before the '/*'-route was the right idea, but the way you did it the router can't assign an action. You could use the following for each controller:
Router::connect('/admin/Menus/:action/*', array('controller' => 'menus', 'prefix' => 'admin'));
Or you could use the default prefix-routing routes, so you don't have to add a route for each new controller.
// prefix routing default routes with admin prefix
Router::connect("/admin/:controller", array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin/:controller/:action/*", array('prefix' => 'admin', 'admin' => true));

Resources