scott
scott

Reputation: 3192

Middleware not working in laravel

I was working on laravel middleware but after log in it return blank page. Following is my code:

route.php

Route::get('/','UsersController@login');
Route::post('dashboard','UsersController@dashboard');
Route::get('admin', ['middleware' => 'admin', function()
{

            Route::get('add-post-new', function () {return view('a.addPost');});

            Route::post('/add-post-new','PostsController@addPost');

            Route::get('/all-post', function () {return view('a.all_post'); });

}

After login, it return to add-post-new page. But it's blank page. Can any one tell where I am doing wrong. Thank you.

Updated

user controller

public function dashboard()
    {
        $rules = array('email'=> 'required|email', 'password' => 'required');
        $validator = Validator::make(Input::all(), $rules);

            if ($validator->fails()) 
            {
                $messages = $validator->messages();
                    return Redirect::to('login')->withErrors($validator);
            }

            $email=Input::get('email');
            $password=Input::get('password');

            if (Auth::attempt(['email' =>$email, 'password' => $password])) 
            {

                return redirect()->intended('admin/add-post-new');

            }
            else
            {
                return Redirect::guest('login')->with('loginFail','Login UnSuccessful !');
            }


    }

login

 @extends('master')

 @section('content')

 <div class="row">
 <div class="col-md-6">
    @if(Session::get('message'))
        <div class="alert alert-success fade in">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>{{ Session::get('message') }}</strong>
        </div>
    @endif
    @if(Session::get('loginFail'))
        <div class="alert alert-danger fade in">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>{{ Session::get('loginFail') }}</strong>
        </div>
    @endif

     <form method="POST" action="dashboard" >
        <div class="form-group @if ($errors->has('email')) has-error @endif">
            <label for="inputEmail">Email</label>
            <input type="email" class="form-control" id="inputEmail" placeholder="Email" name="email">
             @if ($errors->has('email'))  <div class="register-errors">  {{ $errors->first('email') }}</div> @endif
        </div>
        <div class="form-group @if ($errors->has('password')) has-error @endif">
            <label for="inputPassword">Password</label>
            <input type="password" class="form-control" id="inputPassword" placeholder="Password" name="password">
             @if ($errors->has('password'))  <div class="register-errors">  {{ $errors->first('password') }}</div> @endif
        </div>



        <div class="checkbox">
            <label><input type="checkbox"> Remember me</label>
        </div>
        <button type="submit" class="btn btn-primary">Login</button>
        {!! Form::token(); !!}
    </form>

 </div>

 </div>





    @stop

Update 2

This is created new project name of the project final.base URL

localhost/final/public

Route::group('admin',['middleware' => 'admin'], function () {
    Route::get('add-post-new', function () {

        //dd('something');
        return view('a.addPost');

            });

            Route::post('/add-post-new','PostsController@addPost');

            Route::get('/all-post', function () {return view('a.all_post'); });

            Route::get('/add-category', function () { return view('a.addCategory');});

            Route::post('/add-category','CategorysController@addCategory');

});

Then it throw following error:

ErrorException in Router.php line 343:
Argument 1 passed to Illuminate\Routing\Router::group() must be of the type array, string given, called in D:\xampp\htdocs\final\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 216 and defined

If my route group is following:

Route::group(['middleware' => 'admin'], function () {
    Route::get('add-post-new', function () {

        //dd('something');
        return view('a.addPost');

            });

            Route::post('/add-post-new','PostsController@addPost');

            Route::get('/all-post', function () {return view('a.all_post'); });

            Route::get('/add-category', function () { return view('a.addCategory');});

            Route::post('/add-category','CategorysController@addCategory');

});

Then if my url is http://localhost/final/public/admin/add-post-new, error will be:

NotFoundHttpException in RouteCollection.php line 143:

If I run without admin in URL like http://localhost/final/public/add-post-new then it shows add-post-new page

Upvotes: 2

Views: 2420

Answers (2)

Chris Townsend
Chris Townsend

Reputation: 2716

From what I can see you definitely need a route group, as you are currently including routes inside another route which would not work. Change your routes to this

Route::get('/','UsersController@login');
Route::post('dashboard',['as' => 'dashboard', 'uses' => 'UsersController@dashboard']);
Route::group( ['middleware' => 'admin'], function()
{

    Route::get('add-post-new',
        ['as' => 'admin.add-post-new', function () {
                return view('a.addPost');
            }]);

    Route::post('/submit-post-new','PostsController@addPost');

    Route::get('/all-post', function () {
            return view('a.all_post'); 
        });

});

IT also appears that the intended route wouldn't work as it isn't a named route.

public function dashboard()
    {
        $rules = array('email'=> 'required|email', 'password' => 'required');
        $validator = Validator::make(Input::all(), $rules);

            if ($validator->fails()) 
            {
                $messages = $validator->messages();
                    return Redirect::to('login')->withErrors($validator);
            }

            $email=Input::get('email');
            $password=Input::get('password');

            if (Auth::attempt(['email' =>$email, 'password' => $password])) 
            {

                return redirect()->intended('admin.add-post-new');

            }
            else
            {
                return Redirect::guest('login')->with('loginFail','Login UnSuccessful !');
            }


    }

It looks like your form isn't going anywhere also try

<form method="POST" action="{{ route('dashboard') }}" >
        <div class="form-group @if ($errors->has('email')) has-error @endif">
            <label for="inputEmail">Email</label>
            <input type="email" class="form-control" id="inputEmail" placeholder="Email" name="email">
             @if ($errors->has('email'))  <div class="register-errors">  {{ $errors->first('email') }}</div> @endif
        </div>
        <div class="form-group @if ($errors->has('password')) has-error @endif">
            <label for="inputPassword">Password</label>
            <input type="password" class="form-control" id="inputPassword" placeholder="Password" name="password">
             @if ($errors->has('password'))  <div class="register-errors">  {{ $errors->first('password') }}</div> @endif
        </div>



        <div class="checkbox">
            <label><input type="checkbox"> Remember me</label>
        </div>
        <button type="submit" class="btn btn-primary">Login</button>
        {!! Form::token(); !!}
    </form>

Upvotes: 1

Sid
Sid

Reputation: 5843

 Route::group(['middleware' => ['admin']], function () {
       Route::get('add-post-new', function () {
        return view('a.addPost');
       });

       Route::post('/add-post-new','PostsController@addPost');

       Route::get('all-post', function () {
         return view('a.all_post'); 
       }); 
    });

you might want to user Route::group . Try like this once and see what happens

Upvotes: 1

Related Questions