【问题标题】:Apply Middleware auth to all routes except `editPostJob/id` in Laravel将中间件身份验证应用于除 Laravel 中的 `editPostJob/id` 之外的所有路由
【发布时间】:2021-07-03 02:32:54
【问题描述】:

我正在尝试将身份验证中间件应用于除“editPostJob”路由之外的所有路由,但由于 url (http://127.0.0.1:8000/editPostJob/1) 中有一个 ID,因此它不起作用。 每次我尝试访问该链接时,它都会将我重定向到登录页面。

在我尝试过的控制器中:

  public function __construct()
    {
        $this->middleware('auth')->except(['index', 'confirm','editPostJob']);
    }

但它没有工作。 知道我该怎么做吗?

感谢您的帮助。

【问题讨论】:

  • “editPostJob”是路由名称吗?
  • Route::get('editPostJob/{id}', '\App\Http\Controllers\HomeController@yourMethod');
  • Route::get('editPostJob/{id}', '\App\Http\Controllers\HomeController@yourMethod')->name('editPostJob');
  • 嗨 Anurat,我试过了,但还是一样,即使我重启了服务器。
  • 我错了,你只需要方法名,不需要路由名。 ->except(['index', 'confirm', 'yourMethod'])

标签: laravel


【解决方案1】:

我可以通过以下方式解决它:

public function __construct()
{
    $this->middleware('auth')->except(['index', 'confirm', 'yourMethod']);
}

public function yourMethod(Request $request) //select statement
{
    if (\Auth::check() == false) {
        $is_read = "true";
    } else {
        $is_read = strip_tags($this->isLoggerOwnerOfPost($request->id));
    }
    $jobs = DB::table('jobs')->where('job_id', $request->id)->get();
    return view('editPostJob', compact('jobs'))->with('is_read', $is_read);
}

感谢阿努拉特!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-25
    • 2018-07-25
    • 2016-05-10
    • 2017-02-25
    • 2019-12-28
    • 2017-12-05
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多