How does thinkphp determine whether to log in in each method?

WBOY
Release: 2016-08-04 09:20:58
Original
2300 people have browsed it

[tp version 3.2.3]
I wrote a public class CommonController.class.php in Controller, and wrote a constructor method to determine whether to log in, and redirect to the login page if not logged in:

<code><?php
namespace Admin\Controller;

use Think\Controller;

class CommonController extends Controller
{
    public function __construct()
    {
        $isLogin = session('islogin');
        if (!$isLogin) {
            $this->redirect('Login/index');
            return;
        } else {
            session('islogin', $isLogin); // 如果登录刷新一下?不知道是不是这样去刷新,因为可能出现登录了后过期的情况。
        }
    }
}</code>
Copy after login
Copy after login

One problem with this is that it will always redirect when you are not logged in. Error 127.0.0.1 redirected you too many times.
So I changed it to

<code>class CommonController extends Controller
{
    public function isLogin()
    {
        $isLogin = session('islogin');
        if (!$isLogin) {
            $this->redirect('Login/index');
            return;
        } else {
            session('islogin', $isLogin); // 如果登录刷新一下?不知道是不是这样去刷新
        }
    }
}</code>
Copy after login
Copy after login

Then add a constructor to each of the other files that inherit CommonController, such as
ArticleController.class.php

<code><?php
namespace Admin\Controller;

use Think\Controller;

class ArticleController extends CommonController
{
    function __construct()
    {
        parent::isLogin(); // 这样写会报错,不知道为什么,报错提示如下
    }

    public function dolist()
    {

        $this->show("hallo world");
    }
}</code>
Copy after login
Copy after login

Error message:

<code>Call to a member function display() on null
错误位置
FILE: X:\domainX\ThinkPHP\Library\Think\Controller.class.php  LINE: 69</code>
Copy after login
Copy after login

Finally write like this to avoid error:

<code><?php
namespace Admin\Controller;

use Think\Controller;

class ArticleController extends CommonController
{

    public function dolist()
    {
        parent::isLogin(); // 这样写
        $this->show("hallo world");
    }
}
</code>
Copy after login
Copy after login

Question:

  • Why can’t classes that inherit CommonController.class.php be judged in __construct?

  • If a class that inherits CommonController.class.php needs to determine whether to log in, it needs to write parent::isLogin(); in each method. Is this reasonable? Or is it inherently reasonable to do so?

Reply content:

[tp version 3.2.3]
I wrote a public class CommonController.class.php in Controller, and wrote a constructor method to determine whether to log in, and redirect to the login page if not logged in:

<code><?php
namespace Admin\Controller;

use Think\Controller;

class CommonController extends Controller
{
    public function __construct()
    {
        $isLogin = session('islogin');
        if (!$isLogin) {
            $this->redirect('Login/index');
            return;
        } else {
            session('islogin', $isLogin); // 如果登录刷新一下?不知道是不是这样去刷新,因为可能出现登录了后过期的情况。
        }
    }
}</code>
Copy after login
Copy after login

One problem with this is that it will always redirect when you are not logged in. Error 127.0.0.1 redirected you too many times.
So I changed it to

<code>class CommonController extends Controller
{
    public function isLogin()
    {
        $isLogin = session('islogin');
        if (!$isLogin) {
            $this->redirect('Login/index');
            return;
        } else {
            session('islogin', $isLogin); // 如果登录刷新一下?不知道是不是这样去刷新
        }
    }
}</code>
Copy after login
Copy after login

Then add a constructor to each of the other files that inherit CommonController, such as
ArticleController.class.php

<code><?php
namespace Admin\Controller;

use Think\Controller;

class ArticleController extends CommonController
{
    function __construct()
    {
        parent::isLogin(); // 这样写会报错,不知道为什么,报错提示如下
    }

    public function dolist()
    {

        $this->show("hallo world");
    }
}</code>
Copy after login
Copy after login

Error message:

<code>Call to a member function display() on null
错误位置
FILE: X:\domainX\ThinkPHP\Library\Think\Controller.class.php  LINE: 69</code>
Copy after login
Copy after login

Finally write like this to avoid error:

<code><?php
namespace Admin\Controller;

use Think\Controller;

class ArticleController extends CommonController
{

    public function dolist()
    {
        parent::isLogin(); // 这样写
        $this->show("hallo world");
    }
}
</code>
Copy after login
Copy after login

Question:

  • Why can’t classes that inherit CommonController.class.php be judged in __construct?

  • If a class that inherits CommonController.class.php needs to determine whether to log in, it needs to write parent::isLogin(); in each method. Is this reasonable? Or is it inherently reasonable to do so?

<code>    public function _initialize() {
        $allow_actions = explode(',',C('ALLOW_ACTIONS')); //配置哪些操作无需登录即可访问,比如登录,验证登录
        $curr_action = MODULE_NAME . '.' . CONTROLLER_NAME . '.' . ACTION_NAME;
        if(!in_array($curr_action,$allow_actions) && !is_login_admin()) { //未登录且是需要登录后访问的
            $this->redirect('Admin/Public/login');
        }
    }

</code>
Copy after login

Don’t redirect in the public class. The public class only determines whether you are logged in, returns true or false, and then operates based on the returned result.

You write it at the program entrance

Write another login Guestcontroller. Don’t continue to inherit from commonController. Just inherit Controller directly. In this way, before logging in or logging out, the guest’s Guestcontroller will handle it. After logging in, everything is handled by subclasses of commonController.

Idea
Do login verification in __construct of CommonContrller, remember that parent::__construct must be included in __construct

Then each controller inherits this Common, except of course your Login controller, which inherits the Controller under Think

That’s it, you can try it

The Login controller does not inherit. The public controller only determines and does not write login. Isn’t it good?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template