Change admin layout in CakePHP - php

I am working in cakephp, and I have the following two lines in my /app/config/routes.php file:
/**
* ...and setup admin routing
*/
Router::connect('/admin/:controller/:action/*', array('action' => null, 'prefix' => 'admin', 'admin' => true, 'layout' => 'admin' ));
/**
* ...and set the admin default page
*/
Router::connect('/admin', array('controller' => 'profiles', 'action' => 'index', 'admin' => true, 'layout' => 'admin'));
I also have a layout at /app/views/layouts/admin.ctp
However, the layout is not changed when I visit admin URLs

Create a app/app_controller.php and put this in:
<?php
class AppController extends Controller {
function beforeFilter() {
if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
$this->layout = 'admin';
}
}
}
Don't forget to call parent::beforeFilter(); in your controllers if you use it in other controllers.
Semi related to the question, you don't need the routes defined, you just need to enable Routing.admin config option and set it to admin in the app/config/core.php. (CakePHP 1.2)

Add this code in beforeFilter() function in
app_controller.php
<?php
class AppController extends Controller {
function beforeFilter() {
if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
$this->layout = 'admin';
} else {
$this->layout = 'user';
}
}
}
?>
Set layout='admin' in
routes.php
<?php
Router::connect('/admin', array('controller' => 'users', 'action' => 'index','add', 'admin' => true,'prefix' => 'admin','layout' => 'admin'));
?>

For CakePHP 3.X you should edit your src/View/AppView.php file and add the following code to your initialize() method:
public function initialize()
{
if ($this->request->getParam('prefix') === 'admin') {
$this->layout = 'Plugin.layout';
}
}

the approaches above are good but if you are looking to change the layout for every page when logged in you might try the following using Auth Component
function beforeFilter() {
if ($this->Auth->user()) {
$this->layout = 'admin';
}
}

For cakephp 3.0 you can set a view variable by calling Auth->user in the beforeRender in AppController. This is my beforeRender:
public function beforeRender(Event $event)
{
///...other stuff
$userRole = $this->Auth->user();
$this->set('userRole', $userRole['role']);
}

Related

CakePHP: How to use a non-default user model for authentication?

Hi i have a table name chat_users
I have connected users table for last few projects it working fine. But this is my first project i have a different table name chat_users
I want to login this table with username and password
I have tried but unable to login.
Please help me.
Code-
AppController.php
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array('Auth', 'Session', 'Email', 'Cookie', 'RequestHandler', 'Custom');
public $helpers = array('Html', 'Form', 'Cache', 'Session','Custom');
function beforeFilter() {
parent::beforeFilter();
$this->Auth->authenticate = array(
'Form' => array (
'scope' => array('ChatUser.is_active' => 1),
'fields' => array('ChatUser.username' => 'username', 'ChatUser.password' => 'password'),
)
);
}
}
?>
UsersController.php
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public $name = 'Users'; //Controller name
public $uses = array('ChatUser');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login');
}
public function index() {
}
public function login() {
$this->layout='login';
if ($this->request->is('post')) {
if (!$this->Auth->login()) {
$this->Session->setFlash(__('Invalid username or password, try again'), 'error_message');
$this->redirect($this->Auth->redirect());
}
}
if ($this->Session->read('Auth.ChatUser')) {
return $this->redirect(array('action' => 'index'));
exit;
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
}
Above query i am getting missing table.
See screenshot-
Your auth component configuration is incorrect. You are missing the appropriate userModel option, which defines the name of the model to use
And the fields configuration doesn't work the way your are using it, the keys must be named username and password, and the value can then contain the actual column name, however since your columns are obviously using the default names, there's no need to use this option at all.
$this->Auth->authenticate = array(
'Form' => array (
'scope' => array('ChatUser.is_active' => 1),
'userModel' => 'ChatUser'
)
);
Also the session key will always be Auth.User unless you are explicitly changing it via AuthComponent::$sessionKey:
$this->Auth->sessionKey = 'Auth.ChatUser';
However, you are better of using the auth component to access the user data anyways:
// Use anywhere
AuthComponent::user('id')
// From inside a controller
$this->Auth->user('id');
See also
Cookbook > Authentication > Configuring Authentication handlers
Cookbook > Authentication > Accessing the logged in user

Yii disable Model Behavior for one Controller

Im using a behavior(DateTimeI18NBehavior) in Users.php model, but specifically in a controller (ApiController.php) I would like to disable it.
Model - Users.php:
public function behaviors()
{
return array(
'datetimeI18NBehavior'=>array(
'class' => 'ext.DateTimeI18NBehavior',
),
);
}
I know that I can it disable with:
$model->disableBehavior('datetimeI18NBehavior');
But how to disable to entire Controller?
Not sure, but maybe this would work:
class ApiController extends CController
{
function init()
{
Users::model()->disableBehavior('datetimeI18NBehavior');
}
}
Or you can try to add some logic in your model:
function behaviors()
{
if (Yii::app()->controller->uniqueId != 'api') {
return parent::behaviors();
}
return array(
'datetimeI18NBehavior'=>array(
'class' => 'ext.DateTimeI18NBehavior',
),
);
}
Both ways aren't perfect though in my opinion.

Allowing users to access API with CakePHP

I'm building a CakePHP application and I have an API controller. This holds some methods that are common around the site, and I use them with jQuery AJAX calls to do certain things. I recently implemented user registration with the Auth component, but now whenever I try and access the API when I'm not logged in I get redirected to the login page.
This is my AppController code:
class AppController extends Controller {
public $components = array('Session', 'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'images'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home')
));
public function beforeRender() {
$this->set('loggedIn', $this->Auth->loggedIn());
$this->set('username', $this->Auth->user('username'));
}
public function beforeFilter() {
$this->Auth->allow('home', 'register', 'login');
}
}
I know I can allow certain methods within my API controller with the $this->Auth->allow() method, but is there any way to make it controller-wide? For example, is there something I can put in my API controller so non-logged in users can access its methods aswell? I'd rather not put the method names for each action in the allowed list, because there's about 30 of them.
Thanks.
Put this in the ApiController:
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow(); //pass no arguments to allow all
}

Cakephp with basic Auth Component

I try use cakephp with auth component and it's not work.
Somebody can help me, Thank a lot.
this my code:
Controller file here
Class UsersController extends AppController {
var $name = 'Users';
var $uses = array('User');
var $viewPath = 'my_view/user';
var $layout = 'default';
var $helpers = array('javascript');
function beforeFilter() {
parent::beforeFilter();
}
function login() {
$this->layout = 'login';
}
function logout() {
$this->layout = 'login';
$this->redirect($this->Auth->logout());
}
Model File:
Class User extends AppModel {
var $name = 'User';
var $useTable = 'users';
var $belongsTo = array();
var $validate = array(
'username' => array(
'rule' => 'notEmpty',
'message' => 'Username cannot empty'
)
);
}
AppController File:
var $components = array('Auth','Session');
function beforeFilter() {
Security::setHash("md5");
$this->Auth->fields = array('username' => 'username', 'password' => 'password');
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
//$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
$this->Auth->logoutRedirect = '/';
$this->Auth->loginError = 'Invalid e-mail / password combination. Please try again';
$this->Auth->authError = "This error shows up with the user tries to access a part of the website that is protected.";
}
In my view:
echo $this->Session->flash('auth');
echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Login');
from my code, I need it to login to redirect to some page(example: dashboards/index).
And from this code must echo String from $this->Auth->loginError when login fail but it not show then I don't know to solve that. And in my db I store password by md5 too.
Code above I copy and try from Google
Thank again.
I don't know, but if you followed the "simple authentication and authorization" tutorial on the site, you'll have to modify your "routes.php" to also allow access to "display" to get to your /pages/ controller. I have no idea if this is what your problem is, but it kind of seems like it.
Well please put this statement $this->Auth->allow('login'); in your users controllers beforeFilter() callback after the parent::beforeFilter(); call this would allow you to use the login action either you are logged in or not

Can I get the action in the before() method of a controller in Kohana 3?

My controller has...
class Controller_Staff extends Controller {
public function before() {
parent::before();
$id = $this->request->param('id');
$action = $this->request->param('action');
}
public function action_get($id) {
var_dump($id);
}
}
My route is...
Route::set('a', 'bla/<action>/<id>',
array('id' => '\d+', 'action' => '(get|set)'))
->defaults(array(
'controller' => 'staff',
'action' => 'set'
));
When I enter a URL (bla/get/42) which calls Controller_Staff->before() (before calling action_get()), I can access $id in before(), but $action is always NULL.
Is there a better way to access the current $action in the before() method?
Thanks.
Found it!
It ended up being very easy.
$action = $this->request->action;

Categories