简体   繁体   English

使用laravel 4重写路由URL

[英]routing url rewrite with laravel 4

I'm using laravel framework since a few weeks now and i'm building a litle website with it. 从几周以来,我一直在使用laravel框架,并且正在使用它构建一个litle网站。

For now, I have: 现在,我有:

  1. an index page which display a login form 显示登录表单的索引页
  2. an index/auth page which verify the authentication of the user, but doesn't display any view and redirect the user either on the next page, or the first index page 一个索引/身份验证页面,用于验证用户的身份验证,但不显示任何视图并在下一页或第一个索引页面上重定向用户
  3. a page that dislay a list of patient if the user logged in 如果用户登录,则显示患者列表的页面

Here is my problem. 这是我的问题。 My authentication works well, but when I'm redirecting the user to the patient list page, the url become "index/auth". 我的身份验证效果很好,但是当我将用户重定向到患者列表页面时,URL变为“ index / auth”。

My route file looks like : 我的路线文件如下所示:

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'));

My WUsersController looks like : 我的WUsersController看起来像:

    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');
}

this is the "wusers.index" view with the form : 这是“ 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>

@stop @停

and this is my PatientsController : 这是我的PatientController:

    public function getPatientList() {
        return View::make('patients.index', array(
            'title'=>'Patients List',
            'patients'=>DB::table('patient')
                ->join('study', 'patient.pat_id', '=', 'study.sty_pat_id')
                ->select('patient.pat_id', 'patient.pat_name', 'patient.pat_nip', 'patient.pat_birthdate', 'patient.pat_sex', 'study.sty_description', 'study.sty_datetime')
                ->orderBy('study.sty_datetime', 'desc')->take(10)->get())
            );
}

It's been 2 weeks I'm on that problem now, I cannot continue to work on my website till this problem is solved. 我现在已经有2周的时间解决此问题,在解决此问题之前,我无法继续在我的网站上工作。

If you have any question, don't hesitate, and thanks for your help. 如有任何疑问,请不要犹豫,感谢您的帮助。

I suspect the postAuth method is your issue for the following reasons: 我怀疑postAuth方法是您的问题,原因如下:

  • You seem to be getting out two user arrays from your model every login 您似乎每次登录时都会从模型中删除两个用户数组
  • One array is with email addresses that equal the one you entered, Isn't this unique? 一个数组的电子邮件地址等于您输入的电子邮件地址,这不是唯一的吗? If so you could just get the first record rather than all of them 如果是这样,您可以获取第一条记录而不是全部记录
  • The other array is an array of users with the same password. 另一个阵列是具有相同密码的用户阵列。 This seems like very bad practice. 这似乎是非常不好的做法。

Instead what you should be doing is getting out a unique user object via the email address and checking if its password matches the one provided and redirecting accordingly 相反,您应该做的是通过电子邮件地址找到一个唯一的用户对象,并检查其密码是否与提供的密码匹配,并相应地重定向

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');
}

As a side point Laravel has Authentication built in so you could just use that. 另外,Laravel内置了身份验证,因此您可以使用它。 And you probably want to be using something stronger than MD5 for passwords. 您可能希望使用比MD5更强大的密码。

As I was testing things to find out what was my mistake, I realized that, on my index page which display the form, I have two links to "includes" jquery and jquery-mobile. 当我测试事物以找出我的错误时,我意识到,在显示表单的索引页面上,我有两个链接到“ includes” jquery和jquery-mobile。

it looks like: 看起来像:

@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

So, when I remove one of those two .js links, I lose the graphical aspect of my form, but the url is rewrite properly... 因此,当我删除这两个.js链接之一时,我失去了表单的图形外观,但是url被正确重写了...

I don't get it, but the problem seems to come from those .js file. 我不明白,但是问题似乎来自那些.js文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM