webman icon indicating copy to clipboard operation
webman copied to clipboard

How to Add custom extend controller ?

Open haidarvm opened this issue 3 years ago • 1 comments

For Authentication purposes, usually In Codeigniter can create extends

<?php

namespace app\controller;

use app\controller\Auth\Auth;
use support\Request;

class Index extends Auth {

So in Auth add constructor to check if session username exists, else redirect to login It's make easier for admin module Authentication

I did try it but doesn't work

haidarvm avatar Sep 19 '20 00:09 haidarvm

Authority verification should be implemented in middleware. Here is an example.

app/middleware/AuthCheck.php

<?php
namespace app\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

class AuthCheck implements MiddlewareInterface
{
    public function process(Request $request, callable $next) : Response
    {
        $session = $request->session();
        //  your auth check.
        if (!$session->get('userinfo')) {
            return redirect('/user/login');
        }
        return $next($request);
    }
}

Add config to config/middleware.php .

return [
    '' => [
        app\middleware\AuthCheck::class
    ]
];

walkor avatar Sep 19 '20 10:09 walkor