中间件不在laravel中工作

时间:2015-08-21 04:41:50

标签: php

我正在使用laravel中间件,但登录后返回空白页面。以下是我的代码:

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

}

登录后,返回add-post-new页面。但这是空白页。谁能告诉我哪里做错了。谢谢。

更新

用户控制器

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 !');
            }


    }

登录

 @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

更新2

这是项目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');

});

然后它抛出以下错误:

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

如果我的路线组正在关注:

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

});

然后,如果我的网址是 http://localhost/final/public/admin/add-post-new,错误将是:

NotFoundHttpException in RouteCollection.php line 143:

如果我在http://localhost/final/public/add-post-new这样的网址中没有管理员的情况下运行,则会显示add-post-new page

2 个答案:

答案 0 :(得分:1)

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

您可能希望用户Route::group。尝试一次,看看会发生什么

答案 1 :(得分:1)

从我可以看到你肯定需要一个路线组,因为你目前包括在另一条路线内的路线无法使用。将您的路线更改为此

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

});
显然,由于它不是命名路线,因此预计路线也不会起作用。

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 !');
            }


    }

看起来你的表格不会去任何地方尝试

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