- CakePHP 1.3 Application Development Cookbook
- Mariano Iglesias
- 514字
- 2021-04-09 22:04:14
CakePHP's authentication system will provide us with the necessary tools to build a strong, flexible Auth
based application. We can then use it to fetch the current user information and make it available throughout our application.
In this recipe, we will see how to save the current logged-in user's information so it is accessible from any point of our CakePHP application, including its layout, while adding a helpful method to the User
model to make the job easier.
We should have a working authentication system, so follow the recipe, Setting up a basic authentication system.
- Add the following method to your
AppController
class:public function beforeFilter() { $user = $this->Auth->user(); if (!empty($user)) { Configure::write('User', $user[$this->Auth->getModel()->alias]); } }
- Also in your
AppController
class, add the following method inside the class definition:public function beforeRender() { $user = $this->Auth->user(); if (!empty($user)) { $user = $user[$this->Auth->getModel()->alias]; } $this->set(compact('user')); }
- Copy the default CakePHP layout file named
default.ctp
from yourcake/libs/view/layouts
folder to your application'sapp/views/layouts
folder. Place the following code in theapp/views/layouts/default.ctp
folder. While editing this layout, add the following code right where you want login / logout links to appear:<?php if (!empty($user)) { ?> Welcome back <?php echo $user['username']; ?>! <?php echo $this->Html->link('Log out', array('plugin'=>null, 'admin'=>false, 'controller'=>'users', 'action'=>'logout')); } else { echo $this->Html->link('Log in', array('plugin'=>null, 'admin'=>false, 'controller'=>'users', 'action'=>'login')); } ?>
- Add the following method to the
User
model. If you do not have a model created for theusers
table, proceed to create a file nameduser.php
and place it in yourapp/models
directory. If you do have one already, make sure you add theget
method to it:<?php class User extends AppModel { public static function get($field = null) { $user = Configure::read('User'); if (empty($user) || (!empty($field) && !array_key_exists($field, $user))) { return false; } return !empty($field) ? $user[$field] : $user; } } ?>
By storing the user record in an application-wide configuration variable, we are able to obtain the current user information from anywhere in our application, whether it is controllers, components, models, and so on. This gives us the power to know if there's a logged-in user at any point.
We also need to make sure that views are able to learn whether there is a logged-in user. Even though a view could, technically speaking, still have access to the configure variable, it is normally more elegant to set a view variable to avoid any interaction with PHP classes from the view (except for the view helpers).
Finally, we add a handy method to the User
model, so we can obtain the current user from our controllers without having to deal with the Configure
variable. We can also use the get
method to collect a particular bit of user information. For example, to fetch the current user's username from a controller, we would do something like the following:
$userName = User::get('username');
You should not have to load the User
model class yourself, as the Auth
component does it for you.
- 對比Excel,輕松學(xué)習(xí)SQL數(shù)據(jù)分析
- Cinema 4D電商美工與視覺設(shè)計案例教程(培訓(xùn)教材版)
- iPhone Applications Tune/Up
- Salesforce CRM: The Definitive Admin Handbook
- PS App UI設(shè)計從零開始學(xué)
- Apache Maven 3 Cookbook
- Excel公式、函數(shù)與圖表案例實戰(zhàn)從入門到精通(視頻自學(xué)版)
- 中文版Photoshop 2022基礎(chǔ)教程
- ASP.NET 3.5 Social Networking
- Illustrator CC平面設(shè)計標準教程(微課版)
- 抖音+剪映+Premiere短視頻制作從新手到高手(第2版)
- 24小時學(xué)會Word-Excel-PowerPoint 2010三合一
- 原片蛻變:Photoshop CC商業(yè)修圖必修課
- After Effects 影視后期特效:短視頻制作實戰(zhàn)寶典
- Photoshop CC平面設(shè)計實戰(zhàn)從入門到精通