Add admin prefix in usermgmt plugin in cake php - php

i want make separate admin login but not working giving me error for below url
sitename/admin/admin_login
Error:
Error: AdminLoginController could not be found.
Error: Create the class AdminLoginController below in file: app/Controller/AdminLoginController.php
Cake Php routing setting mention below
app\Plugin\Usermgmt\Config\routes.php
Router::connect('/login', array('plugin' => 'usermgmt', 'controller' => 'users', 'action' => 'login')); // working fine
Router::connect('/admin_login', array('admin' => true, 'plugin' => 'usermgmt', 'controller' => 'users', 'action' => 'admin_login')); // not working
app\Config\routes.php
Configure::write('Routing.prefixes', array('admin'));
app\Plugin\Usermgmt\Controller\UsersController.php(action in controller)
public function admin_login() {
// here is admin login code
}
My view file path
app\Plugin\Usermgmt\View\Users\admin_login.ctp

Your route is set up for /admin_login, NOT /admin/admin_login, like the URL you say you're trying.
Just change your URL to sitename/admin_login and it should work.

Related

Login custom route being rejected by Auth

Router::scope('/:club_slug', function ($routes) {
$routes->connect('/login', ['controller' => 'Users', 'action' => 'login']);
});
So when I'm trying access http://example.com/club-name/login, I'm being redirected to http://example.com/users/login with the flash message You have to login to access this area.
Auth loginAction is [controller => 'Users', 'action' => 'login'], since the custom route that I mentioned at beginning of the question is pointing to the path that is specified at loginAction I thought the route will know that I'm talking about the same thing but is not what is happening.
Dynamic route elements are not being added/recognized automatically, you'll either have to persist them using either the persist option (applies only to that specific route):
Router::scope('/:club_slug', function ($routes) {
$routes->connect(
'/login',
['controller' => 'Users', 'action' => 'login'],
['persist' => ['club_slug']]
);
});
or URL filters (affects all routes that are using a club_slug element):
Router::addUrlFilter(function ($params, $request) {
if (isset($request->params['club_slug']) && !isset($params['club_slug'])) {
$params['club_slug'] = $request->params['club_slug'];
}
return $params;
});
or you have to pass the element to your login action manually (this would match the club_slug route regardless of the current URL):
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'club_slug' => 'club-slug' // wherever it may have come from
]
See also
Cookbook > Routing > Creating Persistent URL Parameters
API > Cake\Routing\RouteBuilder::connect()

Does Cakephp Auth can be use even in other controller?

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.

CakePHP Auth loginRedirect error/always redirect to 'users/login' whereas i put different controller

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

Changing the default login setup with CakePHP

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 :)

Admin routing, http://website.com/admin won't go to posts/admin_index

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.

Categories