跳过Laravel 5.3中针对特定Rout的全局中间件

时间:2017-11-14 06:14:50

标签: laravel-5.3

我创建了一个全局中间件,它适用于所有Routs,但现在我需要从该中间件中跳过一个Rout以下是我的MIddleware和Routs以及Kernal.php文件

MIddleware:检查商店

<?php
namespace App\Http\Middleware;
use Closure;
class Checkshop
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{   
    if(session('shop'))
        {
            $shop = session('shop');
        }
    else{
            if($request['shop'])
            {
                session(['shop' => $request['shop']]);
                $shop = session('shop');
            }
            else{
                dd('session expiere');
            }

        }

    return $next($request);
 }
}

Kernal.php

<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
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,
        \App\Http\Middleware\Checkshop::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'cors' => \App\Http\Middleware\Cors::class,
    //'checkshop' => \App\Http\Middleware\Checkshop::class,
 ];
}

Web.php

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('callback', 'callbackController@index')->name('callback');
Route::get('redirect', 'callbackController@redirect')->name('redirect');
Route::get('dashboard', 'dateController@index')->name('dashboard');
Route::any('order','orderController@index')->name('order');
Route::post('editorder/{id}', 'orderController@update')->name('edit');
Route::post('saveconfig','dateController@store');
Route::get('getconfig','dateController@selectdate')->middleware('cors')->name('getconfig');
Route::any('edit/{id}','dateController@update')->name('edit');
Route::get('uninstall', 'callbackController@uninstall')->name('uninstall');
Route::get('donwload-snippet', 'callbackController@download_snippet')->name('donwload-snippet');

我希望通过应用Checkshop MIddleware跳过web.php文件中的getconfig路由。

1 个答案:

答案 0 :(得分:0)

我最近碰到了同样的问题。您可以尝试在中间件本身中处理此问题,而不是尝试在控制器级别处理它。

这可能不是最佳解决方案,但我这样做了。

以下示例,中间件远离&#34;订阅&#34;和&#34;节目&#34;网址。

namespace App\Http\Middleware;

use Closure;

class SampleMiddleware {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

    protected $except_urls = [
        'subscription',
        'programs'               
    ];

    public function handle($request, Closure $next) {

        $regex = '#' . implode('|', $this->except_urls) . '#';

        if (preg_match($regex, $request->path()))
        {
           return $next($request);
        }

        // Rest of your proceedings
    }
}