php CakePHP 检查用户是否在视图中登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5901685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CakePHP check if user is logged in inside a view
提问by Cameron
I have the following code:
我有以下代码:
<?php
if (!$this->Auth->user())
{
echo $this->element('header');
}
else
{
echo $this->element('header-bar');
}
?>
inside my view which should show a different header for logged in users but throws the following error:
在我的视图中,它应该为登录用户显示不同的标题,但会引发以下错误:
Notice (8): Undefined property: View::$Auth [APP/views/layouts/page.ctp, line 17]
Fatal error: Call to a member function user() on a non-object in /Users/cameron/Sites/thehive/app/views/layouts/page.ctp on line 17
How do I fix this? Thanks
我该如何解决?谢谢
回答by meotimdihia
You don't need to do $this->set(compact('authUser'));
only use this in View:
您不需要$this->set(compact('authUser'));
只在 View 中使用它:
if ($this->Session->read('Auth.User')){
// do something
}
回答by jesal
As of CakePHP 2.x:
从 CakePHP 2.x 开始:
<?php if (AuthComponent::user('id')): ?>
Logged in as <?= AuthComponent::user('name') ?>
<?php endif; ?>
回答by webbiedave
Note:Also check out meotimdihia's answer below. It's got a lotof upvotes.
注意:另请查看下面的meotimdihia 的回答。它有很多赞成票。
The Auth component is for use in the Controller. You'll want to check for authorization in the controller, then set a variable for the view, e.g., $this->set('authUser', $this->Auth->user());
. Then in your view you can do:
Auth 组件用于控制器中。您需要检查控制器中的授权,然后为视图设置一个变量,例如,$this->set('authUser', $this->Auth->user());
. 然后在您看来,您可以执行以下操作:
if (!$authUser)
{
echo $this->element('header');
}
If you want this to be done automatically for all controller methods, you can look into modifying cake/libs/controller/app_controller.php
so that it includes the Auth component.
如果您希望为所有控制器方法自动完成此操作,您可以考虑进行修改cake/libs/controller/app_controller.php
以使其包含 Auth 组件。
回答by Mr Griever
To summarize the answers on this page, evaluate one of the following based on which version of CakePHP you are using:
要总结此页面上的答案,请根据您使用的 CakePHP 版本评估以下其中一项:
For version 1.x
对于版本 1.x
$this->Session->read('Auth.User')
For version 2.x
对于版本 2.x
AuthComponent::user('id')
For version 3.x
对于版本 3.x
$this->request->session()->read('Auth.User.id')
回答by Lee Nielsen
This works in Cakephp 3+ (juts modify: "Auth.User.username" to suit your session data)
这适用于 Cakephp 3+(突出修改:“Auth.User.username”以适合您的会话数据)
<?php
if (is_null($this->request->session()->read('Auth.User.username'))) {
echo "....logged out";
} else {
echo "You are Logged in As " . $this->request->session()->read('Auth.User.username');
}
?>
回答by Jeremy S.
its been a while that I have used CakePHP but as far as I can remember CakePHP doesn't support Auth in View. What you can do of course is set a variable in the controller to use it in the view
我使用 CakePHP 已经有一段时间了,但据我所知,CakePHP 不支持视图中的 Auth。您当然可以做的是在控制器中设置一个变量以在视图中使用它
<?
class AppController {
....
function beforeFilter(){
....
$this->set('auth',$this->Auth);
}
....
}
?>
and then use it in the view like this
然后像这样在视图中使用它
$auth->....
or you can use the AuthHelper written by Ritesh Agrawal
或者您可以使用 Ritesh Agrawal 编写的 AuthHelper
http://bakery.cakephp.org/articles/ragrawal/2008/07/29/authhelper
http://bakery.cakephp.org/articles/ragrawal/2008/07/29/authhelper
BTW
顺便提一句
I think if it comes to only test if somebody is logged in @webbiedave's answer is better MVC style wise.
我认为如果只测试是否有人登录 @webbiedave 的答案是更好的 MVC 风格明智。
Nevertheless if you have to access userdata in view the just extract the userinfo from Auth component and set it in the controller as I showed you and use it in the view
尽管如此,如果您必须在视图中访问用户数据,只需从 Auth 组件中提取用户信息并将其设置在我向您展示的控制器中并在视图中使用它
Regards
问候
回答by Spandan Singh
Try this
尝试这个
class AppController extends Controller{
$this->user = false;
public function beforeFilter(){
$this->user = $this->Auth->user();
}
public function beforeRender(){
$this->set('logged_user',$this->user);
}
}
Now You can check $logged_user in the view as
现在您可以在视图中检查 $logged_user 作为
if($logged_user){
// users logged in $logged_user have all the details
}else{
// not logged in
}
回答by YeHtunZ
in cakephp 3 you can check authentication session in the view like this
在 cakephp 3 中,您可以像这样在视图中检查身份验证会话
if($this->request->Session()->read('Auth.User')){
//do when login
}
else{
//do not login
}
回答by infobuster
If it helps anyone out in cakephp version 3.7.8 session has been depriciated to getSession so to update Lee Nielsen's comment
如果它可以帮助任何人在 cakephp 3.7.8 版会话已被贬低为 getSession 所以要更新 Lee Nielsen 的评论
if (is_null($this->request->getSession()->read('Auth.User.username'))) {
echo "....logged out";
} else {
echo "You are Logged in As " . $this->request->getSession()->read('Auth.User.username');
}
回答by Amit Sarwara
I found something worth mentioning, In CakePHP 3.x session handler is deprecated,
我发现值得一提的是,在 CakePHP 3.x 会话处理程序中已弃用,
if we want to access session in view, we can do it via request handler. we have to use
如果我们想在视图中访问会话,我们可以通过请求处理程序来完成。我们必须使用
<?php
// For CakePHP 3.x to access all user information
$this->request->session()->read('Auth.User');
// For CakePHP 3.x to check session
if($this->request->session()->read('Auth.User.id')) {
}
?>