Reputation: 2426
I am using Laravel 5.0
I have a method in MyController
public function myMethod($id) {
dd($id);
}
The routes.php
file
Route::post('path1/{obj-id}/path2', 'MyController@myMethod');
Route::resource('path1', 'MyController');
In the view file, I am calling the method through a form on submit
{!! Form::open(['action' => ['MyController@myMethod', $myObject->id]]) !!}
Now the problem is, every time I click on Submit
I get a 404
error. The URL in the address bar changes to path1/1/path2
as expected, but I get 404
.
Where am I going wrong?
Upvotes: 2
Views: 431
Reputation: 16
The Problem is with your route .
Route::post('path1/{id}', 'MyController@myMethod');
And in View, You missed the Post method . Change your view to below
{!! Form::open(['action' => ['MyController@myMethod', $myObject->id, 'method' => 'post']]) !!}
Hope this helps !
Upvotes: 0
Reputation: 2426
I got the solution myself.
Turns out one cannot have a dash (-
) inside {}
in one's routes.
My route in routes.php
was initially
Route::post('path1/{obj-id}/path2', 'MyController@myMethod');
I changed it to
Route::post('path1/{id}/path2', 'MyController@myMethod');
and now everything works fine.
Sorry for missing the -
in the original question. Thank you all who tried to help.
Upvotes: 2
Reputation: 920
Instead of using {!! Form::open(['action' => ['MyController@myMethod', $myObject->id]]) !!}
, you need to use any of these:
{!! Form::open(['url' => 'path1/'.$myObject->id.'/path2']) !!}
Or, if you use Name routes
i.e. mymethod.update
, you can do it easily with that:
{!! Form::open(['routes' => ['mymethod.update', $myObject->id]]) !!}
BTW, if you really want to use 'action'
, you need to change your url like this in your routes.php
file:
Route::post('path1/path2/{id}', 'MyController@myMethod');
Route::resource('path1', 'MyController');
Hope this help!
Upvotes: 0
Reputation: 3236
You can give your route a name and use this name in form
Route::post('path1/{id}/path2', [
'as' => 'myroute', 'uses' => 'MyController@myMethod'
]);
Now use it at your form like this
Form::open(array('route' => array('myroute', $myObject->id)))
Upvotes: 0