简体   繁体   中英

Passing data from blade view to controller in laravel

I use Form::open(['action' => 'Controller@method']); in blade to pass data from view to controller. Then I got an error:

Action App\\Http\\Controllers\\Controller@method not defined.

That's right since my Controller is at this address:

App\\Modules\\Somethings\\Controllers\\

So, how can I do to fix it.

Thank you.

You can fix it by using routes and not controller@method.

In your routes.php define a resource pointing at your controller

Route::resource('posts', 'PostController', ['before' => 'csrf']);

Then you can use the URL::route() method to get the right route.

{!! Form::open(['url' => URL::route('posts.update', [$post->id]), 'method' => 'put', 'files' => false]) !!}

To see all your routes and how they are aliased run the following artisan command:

L5.x

php artisan route:list

L4.x

php artisan routes

May be the file name is problem. Laravel is basically provide default Controller also. Why not you change the name and try with that new name or full path.

You will get routes file from routes folder also and use laravel 5.3

you can try this way.

  1. first create a route for this in routes.php file

    Route::any('first/second/{id}', [ 'uses' => 'App\\Modules\\Somethings\\Controllers@method' ]);

  2. Then use that routes url in form

    {!! Form::open(['url' => 'first/second/5', 'method' => 'put']) !!}

You can use

Form::model

instead. Example:

Form::model($user, ['method' => 'PATCH', 'action' => ['UserController@update', $user->id], 'files' => true]) 
// do you form stuff
Form::close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM