Chandraprakash
Chandraprakash

Reputation: 25

Skip Global Middleware for specific Rout in Laravel 5.3

I created one global Middleware and it works perfectly fine for all the Routs but now i need to skip one Rout from that Middleware Following is My MIddleware and Routs and Kernal.php file

MIddleware:Check shop

<?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');

I want to skip the getconfig Route in the web.php file from applying the Checkshop MIddleware.

Upvotes: 1

Views: 2243

Answers (1)

mmdush
mmdush

Reputation: 19

I recently bumped into the same kind of issue. You could try handling this in your Middleware itself rather trying to handle it in the controller level.

This may not be the best solution, but I did this.

below example, the Middleware stays away from "subscriptions" and "programs" urls.

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
    }
}

Upvotes: 1

Related Questions