我面对这个错误并且无法找到问题所在,因为它只提到Function()不存在,起初我虽然上限问题,因为函数F是大写但我不是这样的因为我已经浏览过每一行代码很多次,如果有人能指出它的话。
场景是这样的,我在mysql中有一个带有虚拟播种机数据的登录页面。 PHP表单用于验证用户,然后重定向到仪表板页面,但重定向后显示错误页面..
屏幕截图是否有帮助。
https://postimg.org/image/8rrk1vfutn/
CheckRole.php
<?php
namespace App\Http\Middleware;
use Closure;
class CheckRole
{
public function handle($request, Closure $next)
{
$roles = $this->getRequiredRoleForRoute($request->route());
if ($request->user()->hasRole($roles) || !$roles)
{
return $next($request);
}
return redirect()->route('noPermission');
}
private function getRequiredRoleForRoute($route)
{
$actions = $route->getAction();
return isset($actions['roles']) ? $actions['roles'] : null;
}
}
DashboardController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function __construct ()
{
$this->middleware('web');
}
public function dashboard()
{
return view('layout.dashboard');
}
}
LoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $name = 'name';
protected $redirectTo = '/dashboard';
protected $guard = 'web';
public function getLogin()
{
if (Auth::guard('web')->check())
{
return redirect()->route('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attemp(['name'=>$request->name, 'password'=>$request->password,'active'=>1]);
if ($auth)
{
return redirect()->route('dashboard');
}
return redirect()->route('/');
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
}
Authen.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authen
{
public function handle($request, Closure $next, $guard = 'web')
{
if (!Auth::guard($guard)->check())
{
return redirect()->route('/');
}
return $next($request);
}
}
在Kernel.php中添加
'roles' => \App\Http\Middleware\CheckRole::class,
'authen' => \App\Http\Middleware\Authen::class,
Role.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $table = 'roles';
protected $fillable = 'name';
protected $primaryKey = 'id';
public $timestamps = false;
public function users()
{
return $this->hasMany('App\User', 'role_id', 'id');
}
}
user.php的
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password', 'active', 'role_id',
];
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->hasOne('App\Role', 'id', 'role_id');
}
private function checkIfUserHasRole($need_role)
{
return ($need_role == $this->role->name) ? true : null;
}
public function hasRole()
{
if (s_array($roles))
{
foreach($roles as $need_role)
{
if($this->checkIfUserHasRole($need_role))
{
return true;
}
}
}
else
{
return $this->checkIfUserHasRole($roles);
}
return false;
}
}
web.php
<?php
Route::get('/', ['as'=>'/', 'uses'=>'LoginController@getLogin']);
Route::post('/login', ['as'=>'login', 'LoginController@postLogin']);
Route::group(['middleware'=>['authen', 'roles']], function()
{
Route::get('/logout', ['as'=>'logout', 'uses'=>'LoginController@getLogout']);
Route::get('/dashboard', ['as'=>'dashboard', 'uses'=>'DashboardController@dashboard']);
});
UsersTableSeeder
<?php
use Illuminate\Database\Seeder;
use App\User;
class UsersTableSeeder extends Seeder
{
public function run()
{
User::create([
'role_id'=>1,
'active'=>1,
'name'=>'James',
'email'=>'James@yahoo.com',
'password'=>bcrypt('james'),
'remember_token'=>str_random(10)
]);
}
}
RolesTableSeeder
<?php
use Illuminate\Database\Seeder;
use App\Role;
class RolesTableSeeder extends Seeder
{
public function run()
{
Role::insert([
['name'=>'Admin'],
['name'=>'Student'],
['name'=>'Teacher']
]);
}
}
为漫长的代码道歉。
答案 0 :(得分:2)
您忘记在/ login路由中使用。 就像这样
Route::post('/login', ['as'=>'login', 'uses'=>'LoginController@postLogin']);
答案 1 :(得分:1)
对于那些来自谷歌的人来说,我的情况是我打错了
Route::get("document-types", [DocumentTypesController::class], "index");
代替
Route::get("document-types", [DocumentTypesController::class, "index"]);