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' => 'UserController@all']);
Route::get('/create/', [ 'as' => 'users.create', 'uses' => 'UserController@create']);
Route::get('users/login', [ 'as' => 'users.login', 'uses' => 'UserController@getLogin']);
Route::post('users/login', [ 'as' => 'users.login', 'uses' => 'UserController@postSignin']);
Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => 'UserController@getLogout']);
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

Upvotes: 2

Views: 35664

Answers (3)

Tembuck Franco
Tembuck Franco

Reputation: 1

The issue i had was using thesame route url in two different controllers . ie

Route::get('contact/message', 'ContactMessage')->name('contact.message');   and
Route::get('contact/message', 'AboutController')->name('about'); 

so changing any of the url to anything for instance "contact/message" in AboutController was change to "/about" and it work perfectly.

Upvotes: 0

Alejandro De Castro
Alejandro De Castro

Reputation: 2247

in Laravel 8 it's a bit different

Route::get('/users/edit',[UserController::class, 'edit']);

Upvotes: 0

Saiyan Prince
Saiyan Prince

Reputation: 4020

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' => 'UserController@all']);
    Route::get('/create/', [ 'as' => 'users.create', 'uses' => 'UserController@create']);
    Route::get('users/login', [ 'as' => 'users.login', 'uses' => 'UserController@getLogin']);
    Route::post('users/login', [ 'as' => 'users.login', 'uses' => 'UserController@postSignin']);
    Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => 'UserController@getLogout']);
    Route::resource('/','UserController');
});

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

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

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', 'UserController@all');
Route::get('/create', 'UserController@create');
Route::get('users/login', 'UserController@getLogin');
Route::post('users/login', 'UserController@postSignin');
Route::get('users/logout', 'UserController@getLogout');

/* Routes for logged in users */
Route::get('/users/dashboard', 'UserSessionsController@dashboard');
Route::get('/users/edit/{id}', 'UserSessionsController@edit');

And then in your view file:

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

Upvotes: 7

Related Questions