简体   繁体   English

将数据从刀片视图传递到 Laravel 中的控制器

[英]Passing data from blade view to controller in laravel

I use Form::open(['action' => 'Controller@method']);我使用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.未定义 Action App\\Http\\Controllers\\Controller@method。

That's right since my Controller is at this address:没错,因为我的控制器在这个地址:

App\\Modules\\Somethings\\Controllers\\ 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.您可以使用路由而不是 controller@method 来修复它。

In your routes.php define a resource pointing at your controller在你的 routes.php 中定义一个指向你的控制器的资源

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

Then you can use the URL::route() method to get the right route.然后你可以使用 URL::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:要查看所有路由以及它们的别名,请运行以下 artisan 命令:

L5.x L5.x

php artisan route:list

L4.x L4.x

php artisan routes

May be the file name is problem.可能是文件名有问题。 Laravel is basically provide default Controller also. Laravel 基本上也提供默认控制器。 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您还将从路由文件夹中获取路由文件并使用laravel 5.3

you can try this way.你可以试试这个方法。

  1. first create a route for this in routes.php file首先在 routes.php 文件中为此创建一个路由

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

  2. Then use that routes url in form然后在表单中使用该路由 url

    {!! 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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