简体   繁体   English

在公共laravel文件夹中定义目录的路由

[英]define routes for directories in public laravel folder

I have a directory named fileManager in public laravel folder. 我在public laravel文件夹中有一个名为fileManager目录。 now I want to control access it via middlewares So that unsign or not logged users can not access it directly. 现在,我想通过中间件控制对它的访问,以便未签名或未登录的用户无法直接访问它。

By Default any user can type name of directory in browser and navigate to it. 默认情况下,任何用户都可以在浏览器中键入目录名称并导航到该目录。 I want to able to define a route like this that can apply and authentication middleware : 我希望能够定义一个可以应用和认证中间件的路由:

Route::get('/fileManager', ['middleware' => 'auth', function () {
    //
}]);

This is not only, I have many directories in public folder that maybe I want to control users access to those. 这不仅是我在公用文件夹中有许多目录,也许我想控制用户对这些目录的访问。

How Can I do that ? 我怎样才能做到这一点 ?

For create middleware create class in the file 对于创建中间件,在文件中创建类

app/http/middleware 应用/ http /中间件

after create 创建后

add in array to kernel.php your class 将数组添加到kernel.php您的类

    protected $routeMiddleware = [
            'auth' => \App\Http\Middleware\Authenticate::class,
            'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
            'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
            'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

           'newMiddleware' => pahttoclass
        ];

There many ways to do this Laravel. 有很多方法可以做到这一点。 You can choose anyone you like. 您可以选择任何喜欢的人。 But I always use __construct() method in each controller . 但是我总是在每个controller使用__construct()方法。 Just write this block in controller. 只需在控制器中写入该块。

public function __construct()
{
    $this->middleware('auth');
}

It will protect all methods only in this controller from unauthorized user. 它将仅保护该controller所有methods免受未经授权的用户的侵害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM