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).
Related
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
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
I have something that I thought was a relatively common problem, but after researching the issue, it appears not to be as easy as thought.
I have a CakePHP application (using version 1.2.7) and I am trying to change the standard login procedure using the Auth Component. I would like to use a persistent login screen ( like this Jquery plugin : http://web-kreation.com/demos/Sliding_login_panel_jquery/ ) which my users would use to login.
In Cake terminology, I would like to be able to login to the Auth component from the /pages/home screen but Cakephp keeps redirecting to the /users/login.
In My App Controller :
function beforeFilter()
{
...
$this->Auth->loginAction = array( 'controller' => 'users', 'action' => 'login' );
$this->Auth->loginRedirect = array( 'controller' => 'pages', 'action' => 'home' );
$this->Auth->logoutRedirect = array( 'controller' => 'pages', 'action' => 'home' );
$this->Auth->autoRedirect = false;
...
}
If I change the loginAction to /pages/home. the login does not work, in fact it does not even post to the /users/login method. Not exactly sure what has happened.
My question is this :
How do I make a login form located at www.EXAMPLE.com/ which will return to the same location on successful and unsuccessful login?
I would prefer not to have it redirect to /users/login or have that show up in the URL at all.
To change default login URL set by 'Auth'
make Changes in lib/cake/Controller/component/AuthComponent.php
public $loginAction = array(
'controller' => 'users', //Change here
'action' => 'login',
'plugin' => null
);
If you set $this->Auth->autoRedirect to false then you must redirect manually in your login() method. Take a look at this also.
To change where the form submits just changed the submit url in your form, it's that simple.
$form->create('User', array('url'=>array('controller'=>'users','action'=>'login')))
Then you can load your page, and check the action attribute, and you should see your /users/login :)
I have admin routing enabled. How can I set routing, to make http://website.com/admin go to posts/admin_index?
I've got this:
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
But it doesn't seem to work. I get this error (when going to http://website.com/admin):
Missing Controller
Error: Controller could not be found.
Error: Create the class Controller below in file: app/controllers/controller.php
<?php
class Controller extends AppController {
var $name = '';
}
?>
Try a route with:
Router::connect('/admin', array('controller' => 'posts', 'action' => 'index', 'admin' => true));
The default route '/' does not match the URL '/admin', admin routing enabled or not.