Admin Routing in cakephp - php

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.

Related

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.

cakephp - strange issue with routing

I have my app set up to go to Books index page
Here is my address
http://myapp.loc/
Here is router
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
When I go to http://myapp.loc/, the page loads up just fine. But when I click a link within that page that is suppose to link to http://myapp.loc/books/index, it doesn't work.
The web developer tools show
Remote Address:127.0.0.1:80
Request URL:http://myapp.loc/books/index
Request Method:GET
Status Code:404 Not Found
I tried adding a statement in books controller under index action
die("here is my breakpoint");
I see this when the app loads. But if I take it out and let the app load and then click on the link thats basically the same thing, it doesnt even hit that part of the code.
Any help will be appreciated.
Router
<?php
/**
* Here, we are connecting '/' (base path) to controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, /app/View/Pages/home.ctp)...
*/
//Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
Router::mapResources('books', array('id'=>'[0-9A-Za-z]'));
Router::parseExtensions('json');
require CAKE . 'Config' . DS . 'routes.php';
Try to add second line like
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
Router::connect('/books/*', array('controller' => 'books', 'action' => 'index'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Check Manual Here

CakePHP - Routing Using 'admin_' Prefix

I am currently using cake's routes config in order to set up admin views, different from that of the non-admin user. I read the routing chapter of the documentation(cake's), and stumbled upon the prefix routing. Which I thought that it is something I need to use, to accomplish what I need. So I started it with setting up the config/core.php as suggested, and uncommented this
Configure::write('Routing.prefixes', array('admin'));
Then, I added a route in the routes.php :
Router::connect('/admin', array('controller' => 'donors', 'action' => 'index', 'admin' => true));
From what I understood, with the above set, I can define a specific action for the admin, names like : admin_index or admin_view, etc. .
So my AppController has a component set like this :
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'donors',
'action' => 'index'
),
'authError' => 'Access Denied',
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'authorize' => array('Controller')
)
);
So when a non-admin user logs in he should be redirected to 'donors/index', and when the admin logs in I want to redirect him to 'donors/admin_index'.. How can i do this ?
I tried this :
public function beforeFilter(){
if(isset($this->params['admin'])){
$this->layout = 'stafflayout';
$this->Auth->loginRedirect = array(
'controller'=>'donors',
'action'=>'index',
'prefix'=>'admin',
'admin'=>true
);
}
And in the process of testing it out, at first glance I though it worked. but the URL does not change like 'donor/admin_index .. and am still being redirected to donors/index or equivalent, simply to /donors... Why is this not working ?
(seconndary question)Also during the process of testing this out, I changed my the controller and actions of the Auth component LoginRedirect to
'controller'=>'posts'
and
'action'=>'index'
other then 'donors', 'index', and when I logged in, I still got redirected to donors/index.. were it should have redirected me to 'posts/index'
Anyone can help me on these two issues? Primary questions is more important though!
Well the code is fine!
Router::connect('/admin', array('controller' => 'donors',
'action' => 'index', 'admin' => true));
the above will render /donors/index page whenever /admin is written in the url.
Now if you want to add prefix like /donors/admin_index then you have to create one more rule such as:
Router::connect('/donors/admin_index', array('controller' => 'donors',
'action' => 'index', 'admin' => true));
and in beforeFilter function
if(isset($this->params['admin'])){
$this->layout = 'stafflayout';
$this->Auth->loginRedirect = array(
'controller'=>'donors',
'action'=>'admin_index',
'admin'=>true
);
the above code will redirect to /donors/admin_index and routing will render /donors/index page

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

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)

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.

Categories