Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel : Method [show] does not exist

When trying to access this URL 'users/login' I got that error, Here is my code :

View users/login.blade.php :

<head>Sign in : </head>
<body>
{{ HTML::ul($errors->all()) }}
<?php echo Form::open(array('url' => 'users')); 

echo '<div class="form-group">';
    echo Form::label('username', 'User Name');
    echo Form::text('ausername', null, array('class' => 'form-control'));
echo '</div>';

echo '<div class="form-group">';
    echo Form::label('Password', 'Password');
    echo Form::password('apassword', null, array('class' => 'form-control'));
echo '</div>';

echo Form::submit('Sign in', array('class' => 'btn btn-primary'));

echo Form::close();
?>
</body>

Controller Usercontroller.php

<?php

class UserController extends BaseController {


    public function index()
    {
        $users = User::all();
        return View::make('users.index')
            ->with('users', $users);
    }


    public function create()
    {
        return View::make('users.create');
    }


    public function store()
    {
        $rules = array(
            'username'   => 'required|alpha_dash',
            'password'   => 'required|confirmed',
            'name'       => 'required|regex:/^[a-zA-Z][a-zA-Z ]*$/',
            'email'      => 'required|email|unique:users',
            'country'    => 'required'
        );
        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::to('users/create')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
        } else {
            $user = new User;
            $user->username = Input::get('username');
            $user->password = Hash::make(Input::get('password'));
            $user->name = Input::get('name');
            $user->email = Input::get('email');
            $user->country = Input::get('country');
            $user->save();
            // redirect
            Session::flash('message', 'Successfully created user!');
            return Redirect::to('users');
            }
    }


    public function login()
    {
        $reflector = new ReflectionClass("UserController");
        $fn = $reflector->getFileName();
        dd($fn);
        return View::make('users.login');   
    }


    public function authen()
    {
        if (Auth::attempt(array('username' => Input::get('ausername'), 'password' => Input::get('apassword'))))
        {
            return Redirect::intended('users');
        }   
    }


}

and my routes.php

<?php
Route::resource('users','UserController');
Route::get('users/login', 'UserController@login');
Route::get('/', function()
{
    return View::make('hello');
});

is it a route problem, thank you for the help

like image 696
Tarik Mokafih Avatar asked Apr 08 '14 16:04

Tarik Mokafih


Video Answer


2 Answers

I have experienced the same problem as you. The problem ends up with rearranging the resource code, i.e.

Route::get('masterprices/data', 'MasterPriceController@data');
Route::get( 'masterprices/upload', 'MasterPriceController@upload');
Route::post('masterprices/upload', 'MasterPriceController@do_upload');
Route::get('masterprices/{masterprices}/multipledelete', 'MasterPriceController@multipledelete');
Route::resource('masterprices', 'MasterPriceController');

It checks the other possible handler, if none it will reach the last line which is resource to handle index page.

like image 66
Frederick Li Avatar answered Oct 06 '22 17:10

Frederick Li


This one:

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

defines following routes:

| GET|HEAD users               | users.index   | UsersController@index  
| GET|HEAD users/create        | users.create  | UsersController@create 
| POST users                   | users.store   | UsersController@store  
| GET|HEAD users/{users}       | users.show    | UsersController@show   
| GET|HEAD users/{users}/edit  | users.edit    | UsersController@edit   
| PUT users/{users}            | users.update  | UsersController@update 
| PATCH users/{users}          |               | UsersController@update 
| DELETE users/{users}         | users.destroy | UsersController@destroy

So URI users/login calls users.show route and that's the problem.

Solution is like Kryten said to remove that route completely, but I suppose you still want to use some of the routes for the resource, like in your controller (create, store, index), so better use this:

Route::resource('users', 'UserController', ['only'=> ['index','create','store']]);
like image 25
Jarek Tkaczyk Avatar answered Oct 06 '22 17:10

Jarek Tkaczyk