AnchovyLegend
AnchovyLegend

Reputation: 12538

Laravel route not defined error when it is clearly defined

I am trying to handle a basic form with laravel and am running in to an issue where my POST route isn't being detected and is resulting in a route not defined error in the blade template. My goal is to resolve this error and post the form to the controller, then access the various form fields with the $request param.

This is the error: Route [become-a-customer] not defined.

I appreciate any suggestions on how to resolve this.

Form

<form action="{{ route('become-a-customer') }}" method="post" class="col-md-8 offset-md-2">
    <div class="form-row">
        <div class="form-group col-md-6">
            <label for="first_name">First Name</label>
            <input name="last_name" type="email" class="form-control" id="first_name" placeholder="First Name">
        </div>
        ...
    </div>
    <input type="hidden" name="_token " value="{{ Session::token() }}"/>
    <button type="submit" class="btn">SUBMIT</button>
</form>

web.php

Route::post('/become-a-customer', 'BecomeACustomerFormController@postBecomeACustomer');

BecomeACustomerController . php

class BecomeACustomerFormController extends Controller
{
    public function postBecomeACustomer(Request $request)
    {
        $firstName = $request['first_name'];
        $lastName = $request['last_name'];
        ...
        ...
        return redirect()->back();
    }
}

Upvotes: 3

Views: 25659

Answers (7)

OmidDarvishi
OmidDarvishi

Reputation: 650

use this command

php artisan optimize

Upvotes: 10

Abdullah Ahadi
Abdullah Ahadi

Reputation: 11

For me url('routeName') worked instead of route('routeName')

Upvotes: 1

Mr Singh
Mr Singh

Reputation: 112

you can also define as following where "as" key is for naming your route

Route::post('/become-a-customer',  ['uses' => 'BecomeACustomerFormController@postBecomeACustomer', 'as' => 'become-a-customer']);

Upvotes: 1

NIVED KRISHNA
NIVED KRISHNA

Reputation: 41

In Your blade Template, You have used the Named route for the form action but, it is not specified in the route file (Web.php).

Change your route file like this

Route::post('/become-a-customer', 'BecomeACustomerFormController@postBecomeACustomer')->name('become-a-customer');

OR, you have to change the form action like this

action="{{ url('become-a-customer') }}"

Using the named route is the best practice for a Laravel project.

Upvotes: 2

Moshiur
Moshiur

Reputation: 685

route() method uses route name which is undefined. You can define it via name() method on route as below

Route::post('/become-a-customer', 'BecomeACustomerFormController@postBecomeACustomer')->name('become-a-customer');

for more see doucmentation

Upvotes: 0

kmuenkel
kmuenkel

Reputation: 2789

Check your Apache or Nginx configurations. Sometimes a redirect from https to http will alter the method from POST to GET.

I'd recommend setting up a temporary endpoint for GET by the same Route and placing a dd() statement in it to test the theory.

Upvotes: 0

Musa
Musa

Reputation: 1379

Route::post('/become-a-customer', 'BecomeACustomerFormController@postBecomeACustomer')->name('become-a-customer');

Upvotes: 10

Related Questions