【问题标题】:Laravel Change URL First page RoutesLaravel 更改 URL 第一页路由
【发布时间】:2017-11-22 09:22:29
【问题描述】:

我有一个带有用户登录的 Laravel 5.4 网站。 如果我输入 URL,脚本会重定向到登录页面,当然不是“主页”。

如果我输入 URL 而不是登录页面,我现在想要一个新页面。带有登录按钮的页面,然后进入登录页面。我已经创建了一个新的刀片,我希望将其作为第一页:start.blade.php

我该如何改变?

我已经在 web.php 中使用我的代码进行了尝试,但我的代码不起作用:

Route::group(['middleware' => 'auth'],function(){
  Route::get('logout','AuthController@Logout')->name('logout');
  Route::get('/', 'HomeController@index')->name('home');
  Route::get('myprofile','ProfileController@Index')->name('profile');

【问题讨论】:

  • 你想被重定向到新页面??
  • 是的,正确。我想强制脚本在登录前打开“start.blade.php”,作为第一页。

标签: laravel-5


【解决方案1】:

您必须在组外为您的页面添加一条新路由:

Route::get('start','tController@start')->name('start');
Route::group(['middleware' => 'auth'],function(){
  Route::get('logout','AuthController@Logout')->name('logout');
  Route::get('/', 'HomeController@index')->name('home');
  Route::get('myprofile','ProfileController@Index')->name('profile');
}

然后你必须更改Exceptions->Handler.php

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest(route('login')); // <-- change here :)
}

您必须将return redirect()-&gt;guest(route('login')); 更改为您的新路线:)

return redirect()->guest(route('start'));

【讨论】:

    【解决方案2】:

    在您的routes.php 文件中添加一个新的route,而不附加任何中间件。使用这个:-

    Route::get('start','StartController@start')->name('start');

    现在您的 routes.php 文件应该如下所示:-

    // Newly added route for handling pre-login calls.
    Route::get('start','StartController@start')->name('start');
    
    Route::group(['middleware' => 'auth'],function(){
      Route::get('logout','AuthController@Logout')->name('logout');
      Route::get('/', 'HomeController@index')->name('home');
      Route::get('myprofile','ProfileController@Index')->name('profile');
    });
    

    您需要使用名为start 的函数创建一个新控制器StartController,以在不干扰当前代码结构的情况下实现此目的。

    控制器中的启动功能:-

    public function start() {
     return view('< new view name here >');
    }
    

    【讨论】:

    • 谢谢已经添加了,但是如何强制脚本在登录/主页之前打开“开始”页面?
    • 你也可以直接从路由文件中打开视图 - Route::get('/paths/{$path}, function($path){ return view('paths/{$path}/指数); }); e
    • 不要用它重写登录页面,之后就不能再使用了吗?
    • 您能否进一步解释您的评论?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多