简体   繁体   中英

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.

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".

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 :

    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 :

@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 :

    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.

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:

  • 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. And you probably want to be using something stronger than MD5 for passwords.

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.

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...

I don't get it, but the problem seems to come from those .js file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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