I am trying to connect /admin/ to a static page 'admin.ctp'.
I copied the pages controller for modification and copied the display function to admin_display. I also tried creating an admin_index function without parameters. My route looks like this at this moment:
Router::connect('/admin/', array('controller' => 'pages', 'action' => 'index', 'prefix' => 'admin'));
my admin_index function looks like this:
function admin_index() {
$page = 'admin';
$subpage = null;
$title_for_layout = 'Admin';
$this->set(compact('page', 'subpage', 'title_for_layout'));
$this->render('/admin');
}
I put admin.ctp in /views/pages/ and in /views/pages/admin/
Anyway. When I go to /admin/ it redirects me to /. But when I delete admin_index, it complains that the function does not exist, so I does look for it.
Help?
edit: Big correction, all my admin urls go back to /
edit2: resolved it, something with appcontroller :$
Create admin_index.ctp file in /views/pages/.
Remove $this->render('/admin'); from the admin_index function. (If you wanted to use admin.ctp, I think all you would have to do is to remove the / from the argument). There's no reason to render admin.ctp for admin_index, since it's natural for cake to render admin_index.ctp for admin_index function. You just don't gain anything by not doing that the cake way.
If it doesn't work, try
Router::connect('/admin/', array('controller' => 'pages', 'action' => 'index', 'prefix' => 'admin', 'admin' => true));
If you want to route /admin/*action* requests to pages controllers admin_action function, then add this line to routes.php:
Router::connect('/admin/:action/*', array('controller' => 'pages', 'prefix' => 'admin', 'admin' => true));
Router::connect('/admin/', array('controller' => 'pages', 'action' => 'index', 'admin')); would work with the standard pages controller
Related
I'm trying to use my index controller to create a url structure like this: mydomain.com/vehiclemake/vehiclemodel/vehiclemodelyear
I don't know how to alter the default route, or alter a duplicate that will work as intended. Every time I load the page once a make has been added to the url, it gives me a blank screen and the logs tell me it can't find a controller with the name of the vehicle make that was in the url. Below is the default and what I've tried.
Route::set('vehicle', '(/<make>(/<model>(/<model_year>)))')
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
I've tried looking for an answer here on stackoverflow but I haven't found a question that's similar to mine that has an answer.
I'm not sure if I understand your question right.
Anyway, you won't need the first / before .
Besides that, this structure will require a controller for every make and in that controller an action for every model. I think something like the code below will work for the uri: /ferrari/testarossa/1992
class Ferrari extends controller {
public function action_testarossa() {
// whatever you'd like to do
echo $this->request->param('model_year');
}
}
I think the default in routing always is, but I'm not sure.
1. controller
2. action
3. whateveryoudefine
4. whateveryoudefine
Hope this helps!
You must plan your routing so that one URL cannot be matched to 2 routes. I suggest:
Route::set('vehicle', '/<make>(/<model>(/<model_year>))')
->defaults(array(
'controller' => 'vehicle',
'action' => 'index',
));
Route::set('mainpage', '/')
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
Route::set('default', '<controller>(/<action>(/<id>))', ['controller'=>'(index|user)'])
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
Note that I removed 1 pair of brackets. And for default route correct controllers are: index and user.
When i open website with localhost/site_name, url immediately becomes localhost/site_name/default_controller_name.
How can i hide it, so that the url only on that main front page becomes localhost/site_name ?
EDIT this is default controller for front page
public function index() {
$this->set('list', $this->User->Mobilenetwork->find('list', array(
'fields' => array('id', 'network')
)));
if($this->Auth->user() )
{
$this->redirect(array('controller' => 'contacts', 'action' => 'index'));
}
if ($this->request->is('post'))
{
//saving data. this is a mess currently, i need to move majority of this code to the model
}
Have you adjusted your "routes.php" file to make the root url direct to "localhost/site_name/default_controller_name"? If not, go to the routes file within your Config folder and change:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
(which is the default) to this:
Router::connect('/', array('controller' => 'default_controller_name', 'action' => 'index'));
*not sure what version of CakePHP you're running; the above is for 2.4 (though it may be applicable for earlier versions as well).
Recently, I've been studying cake, I've seen the auth library which said to be will take care of the access control over your app, but, it seems like, you can't initialize or even use this auth library when you're not in the 'UsersController', i did not want that, what if it has some admin part wherein i want the URI to be admin/login, or just simply /login, i've been scratching my head over this one, please help.
Another question, why it seems like the functionality of the '$this->redirect' is not effective when i'm putting this one at any method that contains nothing but redirection, or even in the __construct()?
thanks guys, hoping someone could clearly explain to me those things.
you can use the Auth component inside any controller in the application. If you want it will only effect with the admin section then you can add condition in the beforeFilter funciton in you application AppController on Auth initialization like.
// for component initialization.
public $components = array(
'Auth' => array(
'authenticate' => array(
'userModel' => 'Customer', // you can also specify the differnt model instead of user
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
}
and you can bind this on the admin routing like
function beforeFilter(){
// only works with admin routing.
if(isset($this->request->params['prefix']) && ($this->request->params['prefix'] == 'admin')){
$this->Auth->loginRedirect = array('admin' => true, 'controller' => 'pages', 'action' => 'index');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => true);
$this->Auth->loginAction = array('admin' => true, 'controller' => 'customers', 'action' => 'login');
}
}
If you're using cake 2.3.x or later then make sure you have specified the redirect action in correct format like.
return $this->redirect('action_name'); // you can also specify the array of parameters.
Is it possible to have pages route without the controller in the URL but still have other controllers work? Example:
Access pages like this: http://domain.com/about/
Instead of like this: http://domain.com/pages/about/
But still have access to http://domain.com/othercontroller/action/
Doing the following works for having the pages without /pages/ in the URL but if I try to access any other controller it doesn't work:
From: Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
To: Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'index'));
Is there a way to setup the Router so that it runs controller/action if it exists. If it does not it runs the pages controller/action?
I think the short answer is, no - it's not possible in the manner you're hoping*. Routing isn't really "logic" driven, so unless you can come up with a way to match the things you want in both respects you can't do "if controller exists, then _, else _" kind of thing.
*You could, however add each "page" as a row in your routes file. That would allow "about", "contact" ...etc to be accessed directly, while things that don't exactly match them are handled by the remaining routes.
I know I'm late, but here's my tip for someone looking for this.
In routes.php
foreach(scandir('../View/Pages') as $path){
if(pathinfo($path, PATHINFO_EXTENSION) == "ctp"){
$name = pathinfo($path, PATHINFO_FILENAME);
Router::connect('/'.$name, array('controller' => 'pages', 'action' => 'display', $name));
}
}
This will create a route for every ctp file in the View/Pages folder.
I actually solved this the opposite way from Dave's answer above, by adding a route for each controller, rather than each page. (I won't be adding new controllers very often, but I will be adding new content on a regular basis.)
// define an array of all controllers that I want to be able to view the index page of
$indexControllers = array('posts','events','users');
//create a route for each controller's index view
foreach ($indexControllers as $controller) {
Router::connect(
'/' . $controller,
array(
'controller' => $controller,
'action' => 'index'
)
);
}
//create a route to remove 'view' from all page URLs
Router::connect(
'/:title',
array(
'controller' => 'contents',
'action' => 'view'
),
array(
'pass' => array('title'),
'title' => '[a-z0-9_\-]*'
)
);
CakePHP Auth loginRedirect error/always redirect to 'users/login' whereas i put different controller.
I mean, when i open the forbidden page(not allowed/require login)
$this->Auth->allow('index', 'profile', 'view', 'register');
it must redirect to "players/index". I put the loginRedirect to "players",
'loginRedirect' => array('controller' => 'Players', 'action' => 'index'),
but it doesn't work. It always redirect to "users/login" not "players/index" whereas i write "'loginRedirect' => array('controller' => 'Players', 'action' => 'index')".
this is my code:
class AppController extends Controller {
public $components = array(
'Session',
'Auth'=>array(
'loginRedirect' => array('controller' => 'Players', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'Players', 'action' => 'index'),
'authError'=>"Anda tidak dapat mengakses halaman.",
'authorize'=>array('Controller')
)
);
public function isAuthorized($user) {
return true;
}
public function beforeFilter() {
$this->Auth->allow('index', 'profile', 'view', 'register');
$this->set('logged_in', $this->Auth->loggedIn());
$this->set('current_user', $this->Auth->user());
}}
My table's name : players
why the result's always redirect to "users/login" not "players/" or "players/index"?
please tell me why this happens and how i can solve it. Thank you!
I was stuck with the same issue for hours. Set the login action in the beforeFilter of your AppController as following:
$this->Auth->loginAction = array('controller'=>'yourcontollername', 'action'=>'login');
I followed the video youtube.com/watch?v=zvwQGZ1BxdM, see the first reply.
Have you tried to lowercase controller name ? Players => players
'loginRedirect' => array('controller' => 'players', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'players', 'action' => 'index'),
very interesting, i come across a similar problem - after login redirect to the default home page.
I have tried all above methods, but none of them could solve the issue.
finally, i found out that login form did not build properly which action and controller were not set. therefore the html form pointed to '/', when posted.
However, the system still managed to login to right accounts, but none of redirect function worked in this situation.
It might be something you need to look into.
good luck.
The answer lies in the beforeFilter function in AppController.php. You must set allowances for the Auth object.
public function beforeFilter() {
// put in the functions that point to the views you want to be able to see
// without logging in. This works for all controllers so be careful for naming
// functions the same thing. (all index pages are viewable in this example)
$this->Auth->allow('index', 'thePageIWantToSee', 'userAdd', 'landingPage');
}
Simply use the login() function in your Users/Players Controller. With the if cause you can redirect to an diffrent page.
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect('/account'); //$this->redirect($this->Auth->redirectUrl());
}
return $this->redirect( ['controller' =>'pages', 'action' => 'login-fail']);
}
}
Example used in CakePHP 3.2