您好我在hmvc架构中创建了一个项目,其中包含了laravel 4 here中的creolab模块 假设我将我的项目划分为3个模块,例如
--module--
auth
shop
content
此处的场景用户必须首先登录auth模块 之后,他们可以访问2个模块(商店和内容)
当我尝试在模块商店或类似内容中保护路线时
Authenticating Group */
Route::group(array('before' => 'auth'), function() {
Route::get('shop', array(
'as' => 'shop',
'uses' => 'App\Modules\Shop\Controllers\ShopController@getShop'
));
});
我无法访问它,虽然我已成功登录modul Auth
我已经确认我成功登录了这样的返回字符串
我认为问题出在这里 在我的模块帐户中,我的accountController包含这样的脚本
public function postLogin() {
$validator = Validator::make(Input::all(), array(
'username' => 'required',
'password' => 'required'
));
if($validator->fails()) {
return Redirect::route('login.post')
->withErrors($validator);
} else {
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
));
if($auth) {
return "login success";
// return Redirect::intended('shop');
}
else {
return Redirect::route('login')
->with('global', 'Email or Password Not Match');
}
}
}
当我返回简单字符串(禁用重定向)时,我在屏幕上显示login success
,表示我已经成功登录,但是当我主动重定向到另一个模块时,我被推回到登录页面
我在登录页面中使用这个简单的脚本检查身份验证状态,如下所示
@if (Auth::check())
{{ login }}
@else
{{ "not login "}}
@endif
并且没有登录文字
有人能帮助我吗?
@Update
public function postLogin() {
$validator = Validator::make(Input::all(), array(
'username' => 'required',
'password' => 'required'
));
if($validator->fails()) {
return Redirect::route('login.post')
->withErrors($validator);
} else {
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
));
if($auth) {
return Redirect::intended('shop');
}
else {
return Redirect::route('login')
->with('global', 'Email or Password Not Match');
}
}
}
@ 2nd Update 商店模块中的路线
<?php
/* Authenticating Group */
Route::group(array('before' => 'auth'), function() {
Route::get('shop', array(
'as' => 'shop',
'uses' => 'App\Modules\Shop\Controllers\ShopController@getShop'
));
Route::post('shop', array(
'as' => 'shop.post',
'uses' => 'App\Modules\Shop\Controllers\ShopController@postShop'
));
Route::post('shop-delete', array(
'as' => 'shop.delete',
'uses' => 'App\Modules\Shop\Controllers\ShopController@postShopDelete'
));
});
@update my authentication filters.php
/*
|--------------------------------------------------------------------------
| Authentication Filters
|--------------------------------------------------------------------------
|
| The following filters are used to verify that the user of the current
| session is logged into this application. The "basic" filter easily
| integrates HTTP Basic authentication for quick, simple checking.
|
*/
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('login');
}
}
});
答案 0 :(得分:1)
如果有效,请尝试此操作。
if(Auth::attempt(['usernae' => Input::get('username'), 'password' => Input::get('password')]))
{
return 'login success';
}else{
return 'login failed';
}