Laravel 5 忽略身份验证特定路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30590075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:41:42  来源:igfitidea点击:

Laravel 5 ignore Authentication specific route

phpauthenticationlaraveltoken

提问by kevin_marcus

I am new in laravel 5. I have a dashboard page and a login page. whenever I go to localhost:8080/dashboardit always redirect me to localhost:8080/auth/login.

我是 laravel 5 的新手。我有一个仪表板页面和一个登录页面。每当我去localhost:8080/dashboard它总是将我重定向到localhost:8080/auth/login.

I wanted to show my dashboard localhost:8080/dashboardto be viewed without logging in first. Here is my code in VerifyCsfrToken

我想显示我的仪表板localhost:8080/dashboard,无需先登录即可查看。这是我的代码VerifyCsfrToken

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier {

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


    protected $except_urls = [
        'dashboard/dashboard',
    ];

    public function handle($request, Closure $next)
    {
        $regex = '#' . implode('|', $this->except_urls) . '#';

        if ($this->isReading($request) || $this->tokensMatch($request) || preg_match($regex, $request->path()))
        {
            return $this->addCookieToResponse($request, $next($request));
        }

        throw new TokenMismatchException;

        return parent::handle($request, $next);
    }

}

routes.php

路由文件

Route::get('dashboard', 'ReservationController@index');

 Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
 ]);

controller :

控制器 :

use App\reservations;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Console\Scheduling\Schedule;
use Carbon\Carbon;
use Request;

class ReservationController extends Controller {

/*
|--------------------------------------------------------------------------
| Welcome Controller
|--------------------------------------------------------------------------
|
| This controller renders the "marketing page" for the application and
| is configured to only allow guests. Like most of the other sample
| controllers, you are free to modify or remove it as you desire.
|
*/

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');
}

/**
 * Show the application welcome screen to the user.
 *
 * @return Response
 */

public function schedule()
{
    $schedules = schedules::all();


    return view('calendar.schedule',compact('schedules'));
}
public function index()
{
return view('dashboard.dashboard');
}
public function create()
{

    return view('reserve.reserve');
}
public function update()
{
    return view('calendar.update');
}
public function login()
{
    return view('login');
}

public function store(Requests\CreateReservationRequest $request)
{

    $input = Request::all();
    $reservation = new reservations(['user_id'       => '13100024',
                                    'status_id'     => '1',
                                    'room_id'       => $input['room'],
                                    'purpose'       => $input['purpose'],
                                    'start_time'    => $input['date']."        ".$input['hour1'].":".$input['minute1'].":00",
                                    'end_time'      => $input['date']." ".$input['hour2'].":".$input['minute2'].":00",
                                    'add_date'      => Carbon::now()
                                    ]);
    $reservation->save();

    return "success";
   // return redirect('schedule');

   }

回答by Styphon

This is what causes the issue:

这就是导致问题的原因:

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');
}

It restricts access to the page to logged in users. Just remove it from your controller and users will be able to access the page whether they're logged in or not.

它限制登录用户访问页面。只需将它从您的控制器中删除,无论用户是否登录,用户都可以访问该页面。