Php 使用laravel 4重写路由url
我从几周前开始使用laravel框架,现在我正在用它构建一个litle网站 目前,我有:Php 使用laravel 4重写路由url,php,url,laravel,routes,rewrite,Php,Url,Laravel,Routes,Rewrite,我从几周前开始使用laravel框架,现在我正在用它构建一个litle网站 目前,我有: 显示登录表单的索引页 一个索引/身份验证页,用于验证用户的身份验证,但不会在下一页或第一个索引页上显示任何视图并重定向用户 如果用户登录,则显示患者列表的页面 这是我的问题。我的身份验证工作正常,但当我将用户重定向到患者列表页面时,url变成“index/auth” 我的路由文件如下所示: Route::get('index', array('as'=>'index', 'uses'=>'WUs
Route::get('index', array('as'=>'index', 'uses'=>'WUsersController@getIndex'));
Route::post('index/auth', array('uses'=>'WUsersController@postAuth'));
Route::get('patients', array('as'=>'patients', 'uses'=>'PatientsController@getPatientList'));
public function getIndex() {
return View::make('wusers.index')
->with('title', 'Instant Access');
}
public function postAuth() {
$rules = array(
'email'=>'required|email',
'password'=>'required|alphaNum|min:4'
);
$validator = Validator::make(Input::all(), $rules);
$user = WUserModel::where('WUS_EMAIL', '=', Input::get('email'))->first();
if ($validator->fails()) {
return Redirect::route('index')
->withErrors($validator);
}
if ($user->WUS_PASSWORD == md5(Input::get('password'))) {
Session::put('login', $user->WUS_EMAIL);
return Redirect::route('patients')
->with('title', 'Patient List');
}
return Redirect::route('index')
->with('logError', 'login ou mot de passe incorrect');
}
我的Wuser控制器看起来像:
Route::get('index', array('as'=>'index', 'uses'=>'WUsersController@getIndex'));
Route::post('index/auth', array('uses'=>'WUsersController@postAuth'));
Route::get('patients', array('as'=>'patients', 'uses'=>'PatientsController@getPatientList'));
public function getIndex() {
return View::make('wusers.index')
->with('title', 'Instant Access');
}
public function postAuth() {
$rules = array(
'email'=>'required|email',
'password'=>'required|alphaNum|min:4'
);
$validator = Validator::make(Input::all(), $rules);
$user = WUserModel::where('WUS_EMAIL', '=', Input::get('email'))->first();
if ($validator->fails()) {
return Redirect::route('index')
->withErrors($validator);
}
if ($user->WUS_PASSWORD == md5(Input::get('password'))) {
Session::put('login', $user->WUS_EMAIL);
return Redirect::route('patients')
->with('title', 'Patient List');
}
return Redirect::route('index')
->with('logError', 'login ou mot de passe incorrect');
}
这是“wusers.index”视图,其形式如下:
@section('content')
<div id="loginPage" data-role="page">
<div data-role="header">
<h1>InstantAccess</h1>
</div>
<div data-role="content" style="text-align:center">
{{ HTML::image('img/logo_keosys.png') }}
{{ Form::open(array('url'=>'index/auth', 'method'=>'POST', 'id'=>'loginForm')) }}
<p>
{{ $errors->first('email') }}
{{ $errors->first('password') }}
</p>
<div data-role="fieldcontain">
<p>
{{ Form::label('email', 'Email:') }}<br />
{{ Form::text('email') }}
</p>
<p>
{{ Form::label('passorwd', 'Password:') }}<br />
{{ Form::password('password') }}
</p>
</div>
<p>{{ Form::submit('Sign in', array('name'=>'login', 'value'=>'Valider')) }}</p>
@if (Session::has('logError'))
<p style="color: red;">{{ Session::get('logError') }}</p>
@endif
{{ Form::close() }}
</div>
</div>
这是两个星期,我对这个问题现在,我不能继续在我的网站上工作,直到这个问题得到解决
如果您有任何问题,请不要犹豫,谢谢您的帮助。我怀疑postAuth方法是您的问题,原因如下:
- 每次登录时,您似乎都会从模型中获得两个用户数组
- 一个数组的电子邮件地址与您输入的相同,这不是唯一的吗?如果是这样的话,你可以得到第一张唱片,而不是所有的
- 另一个数组是具有相同密码的用户数组。这似乎是非常糟糕的做法
public function postAuth() {
$rules = array(
'email'=>'required|email',
'password'=>'required|alphaNum|min:4'
);
$validator = Validator::make(Input::all(), $rules);
// Early dropout if we have an invalid email
if ($validator->fails()) {
return Redirect::route('index')
->withErrors($validator);
}
$user = WUserModel::where('WUS_EMAIL', '=', Input::get('email'))->first();
if($user->WUS_PASSWORD == md5(Input::get('password'))) {
Session::put('login', $user->WUS_EMAIL);
return Redirect::to('patients')
->with('title', 'Patient List');**
}
return Redirect::route('index')
->with('logError', 'login ou mot de passe incorrect');
}
作为一个侧面点,Laravel内置了身份验证,所以您可以直接使用它。您可能想使用比MD5更强大的密码。当我在测试东西以找出我的错误时,我意识到,在显示表单的索引页面上,我有两个指向“包含”jquery和jquery mobile的链接 它看起来像:
@section('header')
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1"/>
{{ HTML::style('css/jquery.mobile-1.3.0.min.css') }}
{{ HTML::script('js/jquery-1.9.1.min.js') }}
{{ HTML::script('js/jquery.mobile-1.3.0.min.js') }}
@stop
@节(“头”)
{{HTML::style('css/jquery.mobile-1.3.0.min.css')}
{{HTML::script('js/jquery-1.9.1.min.js')}
{{HTML::script('js/jquery.mobile-1.3.0.min.js')}
@停止
因此,当我删除这两个.js链接中的一个时,我丢失了表单的图形部分,但是url被正确地重写了
我不明白,但问题似乎来自那些.js文件。感谢您的优化,我的身份验证仍然有效(而且看起来更好)。我知道这不是一个很好的实践,我知道有一种Auth方法可以用于laravel,但我有一个特定的数据库,用于生产,与laravel驱动程序表名称不匹配。。。(我正在处理)因此我更改了您提供的内容,但重定向用户后,url保持不变,改为“index/auth”,而不是“patients”。从代码看,如果用户正确登录后,您正在设置会话,那么添加一些调试就可以了。如果可以正常工作并获得输出,请在患者控制器中的操作中添加一些日志记录,以查看是否获得输出。好吧,我可以告诉您,如果我使用正确的电子邮件和密码,我将重定向到患者页面,但url是“index/auth”,而不是“patients”。但它是有效的。例如,如果我在登录时提供了错误的信息,我会停留在表单页面上,但是url是“index/auth”而不是“index”。好的,我找到了解决方案。这是因为ajax查询。它在同一个页面上加载页面内容以避免刷新,因此在我的身份验证后会这样做,这就是为什么url保持不变为“index/auth”而不是“patients”。谢谢你的帮助