Laravel路由到控制器中的自定义功能不起作用

时间:2016-09-23 11:12:39

标签: php laravel

laravel 5.3

我试图在AdminController中创建一个按钮或链接执行激活功能,但每当我点击按钮时它会给我404页面,说它无法找到AdminController @ activate。

当我使用函数中的dd()时,它没有做任何事情。

如果您需要任何其他信息,请说明,我绝不是laravel专家: - )

管理中间件

use Closure;
use Illuminate\Support\Facades\Auth;

class admin
{
    /**
    * Handle an incoming request.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Closure  $next
    * @return mixed
    */
   public function handle($request, Closure $next)
   {
       if (Auth::check() && Auth::user()->level == 2)
       {
           return $next($request);
       }
       return abort(401, 'Unauthorized');
   }
}

网络路线

Route::group(['middleware' => ['auth.admin']], function() {
    Route::resource('admin', 'AdminController');
    Route::get('/admin/{id}/delete', 'AdminController@delete');
    Route::get('/admin/activate', 'AdminController@activate');
});

AdminController     

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{       

    public function activate(){
       $users = User::where('active', 0)->get();
       $users->active = 1;
       $users->timestamps = false;
       $users->update();
       $users->timestamps = true;
       return redirect('admin');
   }
}

HTML /刀片

<a href="{{ url('/admin/activate') }}"><span class="glyphicon glyphicon-cog" aria-hidden="true">Activate all</span></a> <br>
    @foreach ($data['inactiveusers'] as $user)
        {{$user->firstname}} {{$user->surname}}
        <a href="{{ url('/admin/' . $user->id .'/edit') }}"><span class="glyphicon glyphicon-cog" aria-hidden="true"></span></a>
        <a href="{{ url('/admin/' . $user->id .'/delete') }}"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>
        <br>
    @endforeach

1 个答案:

答案 0 :(得分:0)

laravel middleware将无法访问受未经身份验证的用户保护的网址。

我该如何解决这个问题?

确保用户已针对您要求的网址进行了身份验证

提示:

将/ admin / activate移出中间件并直接尝试访问。它应该工作

最糟糕的黑客:

您可以从middlewareGroups内的Kernal.php停用此功能,但不建议这样做。