ferenan
ferenan

Reputation: 177

How to use RESTful controller within Laravel 5

I'm working on a form with Laravel 5, following up a tutorial from Openclassropms which is about Laravel 4, and that really gave me a hard time.

Anyway, I'm trying to change these lines in my routes.php file:

Route::get('users' , 'UsersController@getInfos');
Route::post('users', 'UsersController@postInfos');

with this line :

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

but doing so breaks my form, I can still see the input text area but submitting it gives me the following error :

NotFoundHttpException in RouteCollection.php line 145

Here are my controllers and templates:

<?php namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
class UsersController extends Controller {

    public function getInfos()
    {
        return \View::make('infos');
    }

    public function postInfos()
    {
        echo 'The name is ' . Input::get('nom');
    }

}

@extends('tempform')

and

@section('content')
    {!! Form::open(array('url'=>'users')) !!}
        {!! Form::label('nom', 'Enter your name:') !!}
        {!! Form::text('nom') !!}
        {!! Form::submit('Submit') !!}
    {!! Form::close() !!}
@stop

Also, I use a different url once I make the change as stated initially in the tutorial: gappsl/users >> gappsl/users/info

Upvotes: 1

Views: 879

Answers (3)

Raviraj Chauhan
Raviraj Chauhan

Reputation: 653

If you are using Route::controller() method then it is always looking for the functions starting with the HTTP Verb.

In your case, If you want to point the postInfos() through your form. Then you need to change your form definition to this:

{!! Form::open(array('url'=>'users/infos', 'method'=>'POST')) !!}

Or

If you have any confusions regarding which routes to use, then simply go to command line within your application root directory and type php artisan route:list.

There you can find which URI points to which method with full controller path.

Upvotes: 0

Edwin Krause
Edwin Krause

Reputation: 1806

You need to change the line form open to :

  {!! Form::open(array('url'=>'users/post-infos')) !!}

to address your postInfos() function

Upvotes: -1

Maksym
Maksym

Reputation: 3428

You have to create a new method in your controller called getIndex() or postIndex() depending on which HTTP verb you will be using. This way the /users route will work correctly.

Upvotes: 2

Related Questions