我正在使用自定义表创建用户身份验证。在我的登录控制器中,身份验证工作正常并重定向到仪表板。但是,当我要使用新控制器创建另一个 url 时,该控制器的用户身份验证数据不会显示。 我想通过构造函数中的 auth 外观获取用户数据。这怎么可能? 这是我的代码:
网页.php:
<!---Routes for login and dashboard-->
Route::get('/login','CustomLogin@index');
Route::post('/login','CustomLogin@checklogin');
Route::get('/','CustomLogin@SuccessLogin');
Route::get('/logout','CustomLogin@logout');
<!---Routes for other controller where user auth not working-->
Route::get('/add-creditor', 'AddCreditor@index');
CustomLogin.php(控制器):
<?php
namespace App\Http\Controllers;
use App\library\My_functions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\User;
use Illuminate\Support\Facades\Auth;
use Redirect;
use View;
use Session;
use Cookie;
class CustomLogin extends Controller
{
public function __construct()
{
$this->_myFun = new My_functions;
}
public function index()
{
if(!Auth::check()) {
return view('CustomLogin.CustomLogin');
}
else{
Redirect::to(SITE_URL)->send();
}
}
public function username()
{
return 'user_name';
}
function checklogin(Request $request)
{
$this->validate($request, [
'input-username' => 'required',
'input-password' => 'required'
]);
$user_data = array(
'user_name' => $request->get('input-username'),
'password' => $request->get('input-password')
);
if(Auth::attempt($user_data)){
return redirect('/');
}
else
{
return back()->with('error','Wrong Login Details');
}
}
function SuccessLogin(){
if (!$this->_myFun->DoLogIn()) {
Redirect::to(SITE_URL.'login')->send();
}
else {
$data=array();
return View::make('include.dashboard',$data);
}
}
function logout(Request $request){
Auth::logout();
return redirect('/login');
}
}
侃侃尔雅