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

laravel 5.3

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

当我从函数中使用dd()时它不做任何事情 .

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

Admin middleware

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

Web Routes

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

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

2 years ago

laravel middleware将不允许访问受未经身份验证的用户保护的URL .

How can i fix this ?

确保用户已通过您要求的网址的身份验证

Tip :

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

Worst Hack :

您可以在 Kernal.php 中的 middlewareGroups 中禁用此功能,但不建议这样做 .