0

I have seen this post, however I don't believe it is relevant to my issue because I believe I am correctly passing post data through a post route.

Here is the relevant route code:

Route::get('/pass', 'PageController@pass');
Route::post('/pass/{request}',['uses' => 'PageController@passController']);

I would like to have one controller method for the 'pass' page, but to isolate the issue I have separated them.

Here are the relevant methods in PageController.php:

public function pass(){

    return view('pass')->with(array(
        'title'=>'Create A Pass'
    ));



}
public function passRequest($request){
    $data['request'] = $request;
    $validator = Validator::make($request->all(), [
        'studentID' => 'required|max:255',
        'teacherID' => 'required|max:255',
        'destination' => 'required|max:255',
    ]);
    if ($validator->fails()) {
        return redirect('/')
            ->withInput()
            ->withErrors($validator);
    }
    $pass = new Pass;
    $pass->student = DB::table('users')->where('studentID', $request->studentID)->first()->id;
    $pass->teacher = DB::table('users')->where('teacherID', $request->teacherID)->first()->id;
    $pass->destination = $request->destination;
    $pass->save();
    return view('home')->with(array(
        'title'=>'Home',
        'success'=>'null'
    ));
}

I used the method stated here in order to pass data to the controller. If this is bad practice/obsolete I'm open to any suggestions.

This is the form in the 'pass' page responsible for sending the post data

<form action="{{ url('pass') }}" method="POST" class="form-horizontal">

        {!! csrf_field() !!}
        <fieldset>
            <!-- Text input-->
            <div class="container">
                <div class="form-group">
                    <label class="col-md-4 control-label" for="studentID">Student ID</label>
                    <div class="col-md-3">
                        <input id="studentID" name="studentID" type="text" class="form-control input-md">
                    </div>
                </div>
            </div>
            <!-- Text input-->
            <div class="container">
                <div class="form-group">
                    <label class="col-md-4 control-label" for="teacherID">Teacher ID</label>
                    <div class="col-md-3">
                        <input id="teacherID" name="teacherID" type="text" class="form-control input-md">
                    </div>
                </div>
            </div>
            <!-- Text input-->
            <div class="container">
                <div class="form-group">
                    <label class="col-md-4 control-label" for="destination">Destination</label>
                    <div class="col-md-3">
                        <input id="destination" name="destination" type="text" class="form-control input-md">
                    </div>
                </div>
            </div>
            <div class="container">
                <div class="form-group">
                    <div class="col-sm-offset-4 col-sm-6">
                        <button type="submit" class="btn btn-default">
                            <i class="fa fa-check"></i> Create Pass
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
</form>

On submission of this form I get the MethodNotAllowedHttpException Exception.

If a stack trace of the error would be helpful, please let me know. If there are any suggestions on style, I'm open to that as well.

Community
  • 1
  • 1
Damen
  • 101
  • 4

3 Answers3

2

This form tag will generate a POST request to the URL /pass:

<form action="{{ url('pass') }}" method="POST" class="form-horizontal">

Your routes file does not allow that. It only allows GET requests to that url, but POST requests to /pass/{request}.

Not sure if its just a copy/paste mistake, but your POST route is set up to call PageController@passController method, but the method you shared from your controller is named passRequest. Those will need to match also.

Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • Thank you for the response. I think I'm starting to understand the whole routing process. I neglected to connect the get method and {request} variable. Rather than understanding what I was doing I just was copy and pasting example code and using it for that purpose. I can't say it's working yet, at least this issue has been resolved. – Damen May 04 '16 at 13:03
2

In addition to what Jeff Lambert pointed out, you should not put the {request} variable in the route.

You should remove that and have laravel inject the Request object for you.

Import the Request class if you haven't already at the top of the class.

use Illuminate\Http\Request;

And your function should look like the following...

public function passRequest(Request $request)
{
    ...
}

If you have additional parameters to pass through the URL, then you may add them to the route, and add the arguments to the method after Request $request. Laravel will figure out what to do with it.

user1669496
  • 32,176
  • 9
  • 73
  • 65
  • This answer really helped me connect the reason for that {request} variable in the url of the route. Of course it's only for get, because that's where get responses go. Still working out a few issues in my code, but this really helped me connect the dots. Thank you! – Damen May 04 '16 at 13:05
0

try this one...

Route::post('/pass/post','PageController@passController')->name('post_insert');

in your html form change to ...

<form action="{{ route('post_insert') }}" method="POST" class="form-horizontal">

change it also ...

public function passRequest(Illuminate\Http\Request $request){
....
Akbar Soft
  • 1,028
  • 10
  • 19