Ad

Laravel: Route [users.edit] Not Defined

I have created a demo application in Laravel 4, for some learning experience. The routes.php is as follows:

Route::get('/', function()
{
    return View::make('hello');
});

Route::group(['prefix' => 'users', 'before' => 'auth'], function () {
Route::get('dashboard', function () {
    $layout = View::make('users.dashboard');
    $layout->title = "Dashboard";
    $layout->content = View::make('users.dashboard');       
    return $layout;
}); 
Route::get('/all/', [ 'as' => 'users.index', 'uses' => '[email protected]']);
Route::get('/create/', [ 'as' => 'users.create', 'uses' => '[email protected]']);
Route::get('users/login', [ 'as' => 'users.login', 'uses' => '[email protected]']);
Route::post('users/login', [ 'as' => 'users.login', 'uses' => '[email protected]']);
Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => '[email protected]']);
Route::resource('/','UserController');
});

Route::controller('users', 'UserController');

Here, I prefixed the "users". However, Trying logging in with/out correct login details, it shows only the login page instead of showing up the error message on the login page or dashboard.

Any suggestion? FYI,

The index.blade.php in app\views\users is below:

@extends('users.user') 
@section('title', 'User Listing Page')
@section('description', 'This is the user listing page')
@section('main')

<h1>All Users</h1>

<p>{{ link_to_route('users.create', 'Add new user') }}</p>

<h1>Login</h1>
<p>{{ link_to_route('users.login', 'Login') }}</p> 

<table>

    <tr>

        <td>{{ $users->links() }}</td>

    </tr>       

</table>


@if ($users->count())
<table class="table table-striped table-bordered">
    <thead>
        <tr>
    <th>Username</th>
    <th>Email</th>
    <th>Phone</th>
    <th>Name</th>
        </tr>
    </thead>

    <tbody>
        @foreach ($users as $user)
            <tr>
      <td>{{ $user->username }}</td>
      <td>{{ $user->email }}</td>
      <td>{{ $user->phone }}</td>
      <td>{{ $user->name }}</td>
      <td>{{ link_to_route('users.edit', 'Edit', array($user->id), array('class' => 'btn btn-info')) }}</td>
      <td>{{ Form::open(array('method' => 'DELETE', 'route' => array('users.destroy', $user->id))) }}
      {{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
                    {{ Form::close() }}
                </td>
            </tr>
        @endforeach



    </tbody>

</table>

<table>

    <tr>

        <td>{{ $users->links() }}</td>

    </tr>       

</table>

@else
    There are no users
@endif

@stop

The script login.blade.php (on the same location as above) as follows:

@extends('users.user') 

@section('main')

<h1>Login</h1>

{{ Form::open(array('route' => 'users.login')) }}

<ul>


    <li>
        {{ Form::label('username', 'Username:') }}
        {{ Form::text('username') }}
    </li>

    <li>
        {{ Form::label('password', 'Password:') }}
        {{ Form::password('password') }}
    </li>


    <li>
        {{ Form::submit('Submit', array('class' => 'btn')) }}
    </li>
</ul>
{{ Form::close() }}

@if ($errors->any())
<ul>
    {{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
@endif

@stop
Ad

Answer

There is no users.edit route defined in your routes file. Hence, you are bound to receive that error. There's no users.edit route in your code.

Route::group(['prefix' => 'users', 'before' => 'auth'], function () {
    Route::get('dashboard', function () {
        $layout = View::make('users.dashboard');
        $layout->title = "Dashboard";
        $layout->content = View::make('users.dashboard');       
        return $layout;
    }); 
    Route::get('/all/', [ 'as' => 'users.index', 'uses' => '[email protected]']);
    Route::get('/create/', [ 'as' => 'users.create', 'uses' => '[email protected]']);
    Route::get('users/login', [ 'as' => 'users.login', 'uses' => '[email protected]']);
    Route::post('users/login', [ 'as' => 'users.login', 'uses' => '[email protected]']);
    Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => '[email protected]']);
    Route::resource('/','UserController');
});

Add the following route somewhere in your routes file and the error should disappear:

Route::get('/edit/', [ 'as' => 'users.edit', 'uses' => '[email protected]']);

Note: You need to replace the controller method name edit to whatever you have defined.


UPDATE 1

That is because, you have 'before' => 'auth' parameter passed in your Route::group() method. Just remove users from edit route and I hope that should do the job for you.

Just A Side note

I presume that you are new in laravel and have less amount of experience with the framework. I would recommend you to go with this only in order to get your hands dirty with the basics. Then once you start gaining the experience and know how things work behind the scenes, you can jump off to the next level.

If I were at your place, I wouldn't have gone with the code that you have provided, instead I would have kept it way too simple for now. My routes.php file would be like this:

/* Routes for users not logged in. */
Route::get('/all', '[email protected]');
Route::get('/create', '[email protected]');
Route::get('users/login', '[email protected]');
Route::post('users/login', '[email protected]');
Route::get('users/logout', '[email protected]');

/* Routes for logged in users */
Route::get('/users/dashboardtLogin');
Route::post('users/login', '[email protected]');
Route::get('users/logout', '[email protected]');

/* Routes for logged in users */
Route::get('/users/dashboard', '[email protected]');
Route::get('/users/edit/{id}', 'UserSess[email protected]');

And then in your view file:

<a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="{{ url('/users/edit', $user->id) }}">EDIT</a>

Then I would have used the middleware only inside the UserSessionsController file like this:

/**
 * Check if the user is authenticated.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth'); // or any other middleware you have.
}

UPDATE 2:

Learn more about the middleware from the following links:

For version: 5

  1. Official Docs
  2. Matt Stauffer On Middleware

For version: 5.1

  1. Official Docs
  2. Youtube Video

For version: 5.2

  1. Official Docs

Hope this helps you out. Happy Coding. Cheers.

Ad
source: stackoverflow.com
Ad