嗨,我创建了一个项目在HMVC架构与creolab模块在laravel 4这里
比如我把项目分成了3个模块比如这里的
--module--
auth
shop
content
这个场景中用户必须先登录认证模块
之后,他们可以访问剩下的2个模块(商店&内容)
当我尝试在模块商店或内容中验证保护路由时,如以下示例
Authenticating Group */
Route::group(array('before' => 'auth'), function() {
Route::get('shop', array(
'as' => 'shop',
'uses' => 'App'Modules'Shop'Controllers'ShopController@getShop'
));
});
我不能访问它,虽然我已经成功登录模块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更新 shop模块路由
<?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'
));
});
/*
|--------------------------------------------------------------------------
| 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');
}
}
});
如果可以的话试试这个
if(Auth::attempt(['usernae' => Input::get('username'), 'password' => Input::get('password')]))
{
return 'login success';
}else{
return 'login failed';
}