php 在 Laravel 5 中找不到类“App\Http\Controllers\admin\Auth”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28947205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class 'App\Http\Controllers\admin\Auth' not found in laravel 5
提问by Juned Ansari
i am getting error like Class 'App\Http\Controllers\admin\Auth' not found in laravel 5while login. i am new to laravel so please help me or give me some tutorial link for complete laravel application development with admin side
我在登录时收到类似 Class 'App\Http\Controllers\admin\Auth' not found in laravel 5 的错误。我是 Laravel 的新手,所以请帮助我或给我一些教程链接,以使用管理端进行完整的 Laravel 应用程序开发
Routes.php
路由.php
Route::group(array('prefix'=>'admin'),function(){
Route::get('login', 'admin\AdminHomeController@showLogin');
Route::post('check','admin\AdminHomeController@checkLogin');
});
AdminHomeController.php
AdminHomeController.php
<?php namespace App\Http\Controllers\admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminHomeController extends Controller {
//
public function showLogin()
{
return view('admin.login');
}
public function checkLogin(Request $request)
{
$data=array(
'username'=>$request->get('username'),
'password'=>$request->get('password')
);
if(Auth::attempt($data))
{
return redirect::intended('admin/dashboard');
}
else
{
return redirect('admin/login');
}
}
public function logout()
{
Auth::logout();
return redirect('admin/login');
}
public function showDashboard()
{
return view('admin.dashboard');
}
}
login.blade.php
登录名.blade.php
<html>
<body>
{!! Form::open(array('url' => 'admin/check', 'id' => 'login')) !!}
<input type="text" name="username" id="username" placeholder="Enter any username" />
<input type="password" name="password" id="password" placeholder="Enter any password" />
<button name="submit">Sign In</button>
{!! Form::close() !!}
</body>
</html>
回答by Dan Smith
Because your controller is namespaced unless you specifically import the Auth
namespace, PHP will assume it's under the namespace of the class, giving this error.
因为除非您专门导入Auth
命名空间,否则您的控制器是命名空间的,PHP 将假定它位于类的命名空间下,从而出现此错误。
To fix this, add use Auth;
at the top of AdminHomeController
file along with your other use statements or alternatively prefix all instances of Auth
with backslash like this: \Auth
to let PHP know to load it from the global namespace.
要解决此问题,请use Auth;
在AdminHomeController
文件顶部添加其他 use 语句,或者Auth
像这样使用反斜杠作为所有实例的前缀:\Auth
让 PHP 知道从全局命名空间加载它。