Most efficient way to implement a 'logged-in' check with MVC in PHP?

一笑奈何 提交于 2020-01-15 10:41:47

问题


I know questions similar to this have been asked, but I have been searching through the internet and I can't seem to find exactly what I'm looking for.

The most common answer is to put it in the controller. I liked a particular solution from stackoverflow that had a SessionController and NonSessionController, both extending the main controller but with SessionController checking if the user is logged in before the dispatch.

Does this mean that the controller would look something like this?

class SessionController
{
    ...
    function view()
    {
         //view thread stuff
    }

    function post()
    {
         if loggedin then
         {
              //post thread stuff
         }
    }
{

In this situation, it looks like NonSessionController is useless, and that model is only used when every action the controller handles is either strictly for users or non-users, unlike this forum example.

So I guess my question is, is the general concept of the controller above the most efficient way of dealing with login checks when using MVC?


回答1:


I think the idea would be to have one controller which checks the session and login, and one that doesn't.

I would put the login check in the constructor of the session controller so that way every controller which extends it will check the login.

The session controller would look like

class SessionController
{
    public function __construct()
    {
       if ( ! AuthenticationHelper::isLoggedIn() )
       {
           // User is not logged in
           // Do something, maybe a redirect to login page
       }
    }
}

Then you can just extend that controller like

class HomeController extends SessionController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        print "This page checks login status";
    }
}



回答2:


I would make a component. If you're writing your own MVC framework then this will be interesting to see how you implement this.

But, basically you need a class to check for session state. But, if you tie yourself to extending a class for logged in and a class for logged out I'd feel you have too much duplicate code. I also just personally don't think its very intuitive. I'd probably wind up losing track of whether or not a controller should extend the Session or NotSession.

I'm actually in the process of writing my own MVC framework and have thought about how I would solve this problem a little. I haven't gotten to where I've actually implemented code so it's more a working theory at this point. :)

  1. Have one controller base class. That class has a property that we'll call $components. Let's make this an array and it can hold the name of classes for things you want to do in a lot of controllers, but doesn't really belong in the controller itself.

  2. Since I'm using the Front Controller design pattern before the action requested is invoked I will gather the array of $components and load the appropriate class file for each entry.

  3. As each $component file is being loaded I will dynamically add that component object to the controller properties. So that a component with name Session might refer to a class named SessionComponent and you can access it in your controller by using $this->Session->do_something() or $this->SessionComponent->do_something()

I kind-of-sort-of ripped the idea from CakePHP. I use Cake as my production PHP framework and a lot of my ideas for the custom built framework I'm working on is, obviously, inspired by Cake.




回答3:


If you are inside your SessionController, then you shouldn't need to check the loggedin variable for every function, you should do that inside the constructor or the router, if you feel confident enough to manipulate. That way if the user is not logged in, the SessionController file and class would not be loaded at all.




回答4:


Sorry, no english:

base controller

class Controller{


function handleLogin()
{
    if(!Authentication::isLoggedIn())
    {
        //do stuff - redirect to login page?
    }
}

}

someController

class someController extends Controller{


function someAction()
{
    //check login
    $this->handleLogin();

    //do someAction stuff
}

}


来源:https://stackoverflow.com/questions/6550575/most-efficient-way-to-implement-a-logged-in-check-with-mvc-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!