禁用在laravel中的会话表中添加新行

时间:2016-12-20 13:22:30

标签: laravel session

我有laravel 5.0。并将会话驱动程序设置为数据库我有一些链接,不需要在会话表中插入新行。我怎样才能禁止为www.site.com/download插入新行。

2 个答案:

答案 0 :(得分:1)

为无会话访问创建新的路由/中间件类型。通过在Http / Kernel中添加一个不包含StartSession中间件的新中间件组,然后添加一个新的路由文件来保存所有下载链接,然后在{{{ 1}}。

修改RouteServiceProvider中的$middlewareGroups数组,如下所示:

app/Http/Kernel.php

然后编辑protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], 'api' => [ 'throttle:60,1', 'bindings', ], 'sessionless' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], ]; 以映射新创建的路径文件:

app/Providers/RouteServiceProvider

现在在名为Route::group([ 'middleware' => 'sessionless', 'namespace' => $this->namespace, 'prefix' => 'download', ], function ($router) { require base_path('routes/downloads.php'); }); 的{​​{1}}目录中添加一个文件,并在那里添加可下载的路由。如果您想使用通配符来解析他们正在寻找的文件,您可以,或者您可以明确列出将触发下载的路由:

/routes

这不涉及使用无头授权,如果您不希望未经授权访问所有downloads.php路由,则需要使用无头授权。

答案 1 :(得分:0)

此解决方案适用于laravel 5.0 首先必须在app/http/kernel.php中定义两个中间件。第一个中间件是较少的。 lesssession是针对不需要会话的路由。第二是hasssession中间件.hassession适用于需要会话的路由:

<?php namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {

    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    /**
     * The application's route middleware.
     *
     * @var array
     */

    protected $routeMiddleware = [
        'auth' => 'App\Http\Middleware\Authenticate',
        'hassession' => [
            'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
            'Illuminate\Cookie\Middleware\EncryptCookies',
            'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
            'Illuminate\Session\Middleware\StartSession',
            'Illuminate\View\Middleware\ShareErrorsFromSession',
            'App\Http\Middleware\VerifyCsrfToken',
        ],
        'lesssession' => [] ,
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
    ];

}

第2步: 通过编辑app/http/route.php将路线分为两组:

<?php
Route::group(['middleware' => ['lesssession']], function()
{
    Route::get('download', function(){
        // do some stuff for download file
    });
});
Route::group(['middleware' => ['hassession']], function()
{
    // all other route that need session 
});

?>