How to connect to a plugin route on routes.php file CAKEPHP - php

I have a plugin named Manager. I want to map route on /login to the index action of Dashboard Controller on plugin. Something like this:
Router::connect('/login', array('controller' => 'dashboard', 'action' => 'index', 'plugin => 'manager'));
How can I achieve this on Cakephp 2.2?
thanks

Once a plugin has been installed in /app/Plugin, you can access it at the URL /plugin_name/controller_name/action. In your Manager plugin example, you'd access DashboardController at /manager/dashboard.
Have you tries using Plugin Syntax, like:
Router::connect('/login',
array('controller' => 'Manager.dashboard', 'action' => 'index')
);
Using the 'plugin' key in the $options array should also do the job:
Router::connect('/login',
array('plugin' => 'manager','controller' => 'dashboard', 'action' => 'index')
);

If use plugin like 'users plugin' try in plugin's routes file (routes.php)

Related

Reverse Routing in cakePHP 2

I have created URLs in cake 2.x using strings like
$this->Html->link('View', '/posts/view/' . $id)
//posts/view/id
in multiple times And then later decided that /posts should be called /articles instead in URL.
//articles/view/id
I don't want to change my existing code, I want to use Reverse Routing.
I read about the reverse routing but could not find any suitable example
related to my problem.
It will be appreciated if anybody gives me solution regarding my problem ?
Dont hard code the urls, use arrays instead
$this->Html->link('View', array('controller' => 'posts', 'action' => 'view' ,$id));
And in your route file (app/Config/routes.php), specify the routing rule
//The indexaction
Router::connect(
'/articles',
array('controller' => 'posts', 'action' => 'index')
);
//The view action
Router::connect(
'/articles/:id',
array('controller' => 'posts', 'action' => 'view'),
array(
'pass' => array('id'),
'id' => '[0-9]+'
)
);

Cakephp routes redirect base path

I am trying to make an URL like these:
www.website.com/,
www.website.com
redirect to
www.website.com/members/login
through routes.php.
I have this at the moment: Router::connect('/', array('controller' => 'home', 'action' => 'index'));
How can i setup the route / to reach my desired url?
Thank you!
You can just replace the line:
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
with:
Router::connect('/', array('controller' => 'members', 'action' => 'login'));
However, I believe you don't want to do this.
Just leave your routes untouched, and set up AuthComponent properly:
class AppController extends Controller {
// Pass settings in $components array
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'members',
'action' => 'login',
),
//[...] rest of your Auth options
)
);
For further reference, see Cookbook 2.x: Authentication.
Probably you have AuthComponent somewhere and '/' is set as dissallowed.
$this->Auth->allow('/') should help.

Admin Routing in cakephp

Im trying to put my app under /admin on cakephp. And i am trying to configure the admin routing. What im trying to achive is this:
Lets say the page is www.example.com so when the user type www.example.com/admin i want him/her to be redirected to the admin_dashboard.ctp (if it is logged in, otherwise redirect to log-in page). But now the problem is when i type www.example.com/admin it shows an error like:
Action PagesController::admin_index() could not be found
but if i do:
www.example.com/admin/users/dashboard it is redirected properly.
How can i achieve that? so just by typing /admin to redirect to dashboard??
and another thing is it possible to remove /users/ from url and just display admin/dashboard?
On core.php file i have added the following line:
Configure::write('Routing.prefixes', array('admin'));
And on the routes.php i have these lines :
Router::connect('/', array('controller' => 'users', 'action' => 'dashboard', 'dashboard'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/* I added this line for admin routing */
Router::connect('/admin', array('controller' => 'pages', 'action' => 'index', 'admin' => true));
In routes.php write
Router::connect('/admin', array('controller' => 'users', 'action' => 'dashboard', 'admin' => true));
instead of
Router::connect('/admin', array('controller' => 'pages', 'action' => 'index', 'admin' => true));
This redirect you to admin_dashboard when you type www.example.com/admin.
Routing prefixes are added to the action name when looking for its corresponding method in the Controller.
In the Pages Controller rename your index() method into admin_index() as suggested by the error you are getting.

kohana custom routes

I am having some trouble coming up with the routes for the following scenario...
I have a module controller in say...
/modules/mymodule/classes/controller/mymodule.php (class Controller_Mymodule)
and the url being
/mymodule/
and then I want to have the admin controller
/modules/mymodule/classes/controller/admin/mymodule.php (class Controller_Admin_Mymodule)
but the url would be
/admin/mymodule/
I am trying this route below but I am getting the error: Unable to find a route to match the URI: admin
Route::set('admin', 'admin/<controller>(/<action>(/<id>))')
->defaults(array(
'directory' => 'admin',
'controller' => 'pages',
'action' => 'index',
));
Unable to find a route to match the
URI: admin
Does it mean that admin/mymodule works? Anyway, admin will failed because your route has required controller param. Here is the same route with optional controller segment:
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'pages',
'action' => 'index',
));
PS. You can skip action param because 'index' is a default value.

Kohana 3: Routing with subdirectories error, controller does not exist

So I'm trying to build a route with sub directories and following the Kerkness wiki guide but keep getting errors. If someone could point out what I'm doing wrong I would greatly appreciate it.
http://kerkness.ca/wiki/doku.php?id=routing:building_routes_with_subdirectories
The code:
Route::set('default', '(<directory>(/<controller>(/<action>(/<id>))))', array('directory' => '.+?'))
->defaults(array(
'directory' => 'admin',
'controller' => 'main',
'action' => 'index',
));
The url:
/admin/weather/feedback
The file:
/application/classes/controller/admin/weather/feedback.php
class Controller_Admin_Weather extends Controller_Admin_Base {
The error:
ReflectionException [ -1 ]: Class controller_admin_weather does not exist
Weather needs to be the controller not feedback. Make a weather.php in the admin folder and put the controller as Controller_Admin_Weather and then the action action_feedback.
As #mikelbring said, your controller class is named wrongly. A class in that file should be called Controller_Admin_Weather_Feedback
Do you really need so many optional segments in your route?
Also; if there are no variable elements to the urls you can just stick with defaults like this:
Route::set('my_route_name', 'admin/weather/feedback')
->defaults(array(
'directory' => 'admin/weather',
'controller' => 'feedback',
'action' => 'index',
));
If your class was in /application/classes/controller/admin/weather.php and had an action_feedback(...) method, you could use the following route
Route::set('my_route_name', 'admin/weather/feedback')
->defaults(array(
'directory' => 'admin',
'controller' => 'weather',
'action' => 'feedback',
));

Categories