Error in laravel 4

50 views Asked by At

When i try to log in , it redirects me to www.example.com/admin/{username} but it shows a NotFoundHttpException error. Thank you in advance!

The routes.php file

Route::get('/admin', 'SessionsController@create');  

Route::get('/logout', 'SessionsController@destroy');  

Route::get('profile', function()
{
    return "welcome! Your username is" . Auth::admin()->username;
});  

Route::resource('sessions', 'SessionsController', ['only' => ['index',   'create', 'destroy', 'store']]);

here is the controller SessionsController.php

<?php

class SessionsController extends \BaseController {

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    return View::make('admins');
}


/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */

public function store()
{

    $rules = array('username' => 'required', 'password' => 'required');

    $validator = Validator::make(Input::all(), $rules);

    if($validator -> passes()){
        $credentials = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );
        if(Auth::admin($credentials,true)){
            $username = Input::get('username');
            return Redirect::to("admin/{$username}");
        } else {
            return Redirect::to('/admin')->withErrors('Username or password invalid');
        }
    } else {
        return Redirect::to('/admin')->withErrors($validator->messages());

}
}


/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    Auth::logout();

    return Redirect::home();
}

}

the admins.blade.php

{{Form::open(array('route' => 'sessions.store'))}}
<h1>ADMIN LOGIN </h1> 
{{Form::label('username', 'Username')}}
{{Form::text('username')}}
{{Form::label('password', 'Password')}}
{{Form::password('password')}}
{{Form::submit('Login')}}
{{$errors->first()}}

{{Form::close()}}

and here is the model Admin.php

    use Illuminate\Auth\UserTrait;
    use Illuminate\Auth\UserInterface;
    use Illuminate\Auth\Reminders\RemindableTrait;
    use Illuminate\Auth\Reminders\RemindableInterface;

    class Admin extends Eloquent implements UserInterface, RemindableInterface {


        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = array('password', 'remember_token');

        /**
         * The database table used by the model.
         *
         * @var string
         */
       protected $table = 'admins';



    }

I also installed ollieread multiauth and here is auth.php file

    return array(

        'multi' => array(
            'admin' => array(
                'driver' => 'database',
                'model'  => 'Admin',
                'table'  => 'admins'
            ),

            'user' => array(
                'driver' => 'eloquent',
                'model'  => 'User',
                'table'  => 'users'
            )
        ),

        'reminder' => array(

            'email' => 'emails.auth.reminder',

            'table' => 'password_reminders',

            'expire' => 60,

        ),

    );
1

There are 1 answers

2
Harry Geo On BEST ANSWER

You haven't declared any routes pointing to /admin/{username} . That's the reason it can't be found by laravel.

Here is an example:

In your route files

Route::get('/admin/{username}', 'SessionsController@getByUserName');

I strongly advice you to use named routes. They are a lot easier to manage, even if your application gets bigger.

For example

In your route files

Route::get('/admin/{username}',array('as' => 'admin.username', 'uses'=>'SessionsController@getByUserName'));

Then you can call the url that points to the certain controller either using

URL::route('admin.username')(inside views) or

Redirect::route('admin.username') (inside controller)

You can also pass variables using an array. like this

URL::route('admin.username',array($user->username))

More about routing here