Laravel 控制器构造函数异常

问题描述 投票:0回答:1

您好,我试图允许一个视图无需身份验证,这意味着任何未登录系统的人都应该能够访问该视图,而无需重定向到登录页面。我认为代码应该没问题,但系统仍然将我重定向到登录视图。请帮忙检查我的代码:

<?php

namespace App\Http\Controllers;

use App\Records;
use Illuminate\Http\Request;

class HomeController extends Controller
{
/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
   // $this->middleware('auth');
    $this->middleware('auth')->except(['welcome']);
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{

    $records=Records::paginate(5);

    return view('home',['records'=>$records]);
}
public function prelogin()
{

    $records=Records::paginate(5);

    return view('welcome',['records'=>$records]);
}
}

web.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('/', 'HomeController@prelogin');

Route::post('/create', 'RecordController@create');
Route::get('/viewrec/{id}', 'RecordController@listRec');
Route::get('/editrec/{id}', 'RecordController@editRecords');
Route::post('/update/{id}', 'RecordController@updateRec');
Route::get('/deleteRec/{id}', 'RecordController@destroyRec');
/*Route::get('/upload', function(){
return view('upload')
});*/
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

它一直在登录,我不知道为什么它没有显示欢迎视图。

        <!doctype html>
    <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">

            <title>Laravel</title>

            <!-- Fonts -->
            <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css">

            <!-- Styles -->
            <style>
                html, body {
                    background-color: #fff;
                    color: #636b6f;
                    font-family: 'Nunito', sans-serif;
                    font-weight: 200;
                    height: 100vh;
                    margin: 0;
                }

                .full-height {
                    height: 100vh;
                }

                .flex-center {
                    align-items: center;
                    display: flex;
                    justify-content: center;
                }

                .position-ref {
                    position: relative;
                }

                .top-right {
                    position: absolute;
                    right: 10px;
                    top: 18px;
                }

                .content {
                    text-align: center;
                }

                .title {
                    font-size: 84px;
                }

                .links > a {
                    color: #636b6f;
                    padding: 0 25px;
                    font-size: 13px;
                    font-weight: 600;
                    letter-spacing: .1rem;
                    text-decoration: none;
                    text-transform: uppercase;
                }

                .m-b-md {
                    margin-bottom: 30px;
                }
            </style>
        </head>
        <body>
            <div class="flex-center position-ref full-height">

                    <div class="top-right links">



                            <a href="{{ route('login') }}">Login</a>


                                <a href="{{ route('register') }}">Register</a>

                    </div>


                <div class="content">
                    <div class="title m-b-md">
                        Laravel
                    </div>
                    <div>
                            <table clas="table table-hover table-bordered">
                                    <thead>
                                    <tr>
                                        <th>Record ID</th>
                                        <th>Respond ID</th>
                                        <th>Start Date</th>
                                        <th>End Date</th>
                                        <th>Created at</th>

                                    </tr>
                                    </thead>

                                    @foreach($records as $record)
                                        <tr>
                                            <td>{{$record->id}}</td>
                                            <td>{{$record->col1}}</td>
                                            <td><a href="/viewrec/{{$record->id}}">{{$record->col2}}</a></td>
                                            <td>{{$record->col3}}</td>
                                            <td>{{$record->created_at->format('d/m/y H:i')}}</td>

                                        </tr>
                                    @endforeach
                                </table>
                                {{$records->links()}}
                    </div>

                    <div class="links">
                        <a href="https://laravel.com/docs">Documentation</a>
                        <a href="https://laracasts.com">Laracasts</a>
                        <a href="https://laravel-news.com">News</a>
                        <a href="https://nova.laravel.com">Nova</a>
                        <a href="https://forge.laravel.com">Forge</a>
                        <a href="https://github.com/laravel/laravel">GitHub</a>
                    </div>
                </div>
            </div>
        </body>
    </html>
php laravel controller
1个回答
2
投票

您必须将中间件分配给控制器操作,而不是视图名称。

因此,在您的情况下,您必须将

prelogin
添加到 except 函数中:

$this->middleware('auth')->except('prelogin');
© www.soinside.com 2019 - 2024. All rights reserved.