phplaravelcontroller

Laravel controller constructor exception


Hi I was trying to allow one view no need auth, that means anyone without login the system should be able to visit the view without redirecting to the login page. I think the code should be ok, but still system is redirecting me to login view. please help check my code:

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

It keeps logging in, I has no idea why it's not showing welcome view.

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

Solution

  • You have to assign the middleware to the controller actions, not the view names.

    So in your case you have to add prelogin to the except function:

    $this->middleware('auth')->except('prelogin');