【问题标题】:Middleware not working in laravel中间件在 laravel 中不起作用
【发布时间】:2015-11-14 22:27:00
【问题描述】:

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

路由.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 这样的 URL 中没有管理员运行,那么它会显示 add-post-new page

【问题讨论】:

  • 您是否尝试使用中间件保护路由?
  • @Sid.yes.i 正在为多个用户做。但这里我只是为 admin 做。登录后它必须重定向到 admin/一些路由
  • @Sid. 登录后重定向到 localhost/laravel-news/public/admin/add-post-new 但它返回空白页
  • 使用组作为中间件,它会解决问题。因为 Route::group(['middleware'=>'admin'],function(){ //在这里列出你的路由。});
  • @Muhammad Sadiq.nope 同样的问题

标签: php


【解决方案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'); 
        });

});

IT 似乎还认为预期的路线不起作用,因为它不是命名路线。

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>

【讨论】:

  • 。我已经添加了您的代码,但现在它返回的登录页面也是空白
  • 还要更改添加帖子的获取方式,因为它们是相同的。我将在上面的代码中进行调整
  • 是的,调试是真的。'debug' => env('APP_DEBUG', true),
  • 在你的 env 文件中设置为 true 吗?
  • yes .APP_DEBUG=true.even 我试过你更新的代码但没有用
【解决方案2】:
 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 。像这样尝试一次,看看会发生什么

【讨论】:

  • 如果我更改为组然后也返回空白页。我尝试使用 php artisan 缓存:也清除但没有用
  • @tester 你能发布你验证用户登录的控制器吗
  • 尝试将此路由 Route::get('add-post-new', function () {return view('a.addPost');}); 移出您的管理员身份验证路由并导航到它以缩小和隔离问题
  • 它在我们的中间件管理中。
  • @mdamia.如果我删除中间件管理组然后我可以看到我的主页意味着添加后新。如果我添加中间件然后它返回空白页
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 2020-03-16
  • 2018-07-21
  • 2015-06-30
相关资源
最近更新 更多