Html 在laravel的html中添加表单动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28984369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:39:39  来源:igfitidea点击:

Adding form action in html in laravel

htmlformsurllaravelaction

提问by Shahid Rafiq

I am unable to pass url in views html form action tag.

我无法在视图 html 表单操作标签中传递 url。

<form method="post" action="??what to write here??" accept-charset="UTF-8">

I want to set it's action to WelcomeController@log_infunction in WelcomeControllerfile in controllers.

我想将它的动作设置为在控制器的文件中WelcomeController@log_in起作用WelcomeController

Here are my routes:

以下是我的路线:

Route::get('/','WelcomeController@home');
Route::post('/', array('as' => 'log_in', 'uses' => 'WelcomeController@log_in'));
Route::get('home', 'HomeController@index');

After submitting it keeps the same url

提交后它保持相同的网址

http://localhost:8000/

And the main error line

和主要的错误行

Whoops, looks like something went wrong.

After that there is 1/1 TokenMismatchException in compiled.php line 2440:

之后有 1/1 TokenMismatchException in compiled.php line 2440:

回答by lukasgeiter

You can use the action()helper to generate an URL to your route:

您可以使用action()帮助程序生成路由的 URL:

<form method="post" action="{{ action('WelcomeController@log_in') }}" accept-charset="UTF-8">

Note that the Laravel 5 default installation already comes with views and controllers for the whole authentication process. Just go to /homeon a fresh install and you should get redirected to a login page.

请注意,Laravel 5 默认安装已经带有用于整个身份验证过程的视图和控制器。只需/home进行全新安装,您就会被重定向到登录页面。

Also make sure to read the Authentication section in the docs

还要确保阅读文档中身份验证部分



The error you're getting now (TokenMismatchException) is because Laravel has CSRF protection out of the box

你现在得到的错误 ( TokenMismatchException) 是因为 Laravel 具有开箱即用的 CSRF 保护

To make use of it (and make the error go away) add a hidden field to your form:

要使用它(并使错误消失),请在表单中添加一个隐藏字段:

<input name="_token" type="hidden" value="{{ csrf_token() }}"/>

Alternatively you can also disable CSRF protection by removing 'App\Http\Middleware\VerifyCsrfToken'from the $middlewarearray in app/Http/Kernel.php

或者,您也可以通过'App\Http\Middleware\VerifyCsrfToken'$middleware阵列中删除来禁用 CSRF 保护app/Http/Kernel.php

回答by Mau Xanh Cua Linh

Laravel 5.8 Step 1: Go to the path routes/api.php add: Route::post('welcome/login', 'WelcomeController@login')->name('welcome.login'); Step2: Go to the path file view

Laravel 5.8 第一步:到路径 routes/api.php 添加:Route::post('welcome/login', 'WelcomeController@login')->name('welcome.login'); Step2:进入路径文件查看

<form method="POST" action="{{ route('welcome.login') }}">
</form>

Result html

结果 html

<form method="POST" action="http://localhost/api/welcome/login">

<form>

回答by Jalpesh Khakhi

if you want to call controller from form action that time used following code:

如果您想从当时使用以下代码的表单操作中调用控制器:

<form action="{{ action('SchoolController@getSchool') }}"  >

Here SchoolControlleris a controller name and getSchoolis a method name, you must use getor postbefore method name which should be same as in form tag.

这里SchoolController是一个控制器名,getSchool一个方法名,你必须在方法名之前使用getor post,它应该与表单标签中的相同。

回答by shalini

1) In Laravel 5 , form helper is removed .You need to first install laravel collective .

1) 在 Laravel 5 中,移除了表单助手。您需要先安装 Laravel 集合体。

Refer link: https://laravelcollective.com/docs/5.1/html

参考链接:https: //laravelcollective.com/docs/5.1/html

{!! Form::open(array('route' => 'log_in')) !!}

OR

或者

{!! Form::open(array('route' => '/')) !!}

2) For laravel 4, form helper is already there

2)对于laravel 4,表单助手已经存在

{{ Form::open(array('url' => '/')) }}

回答by FireFistMedia

Use action="{{ action('WelcomeController@log_in') }}"

action="{{ action('WelcomeController@log_in') }}"

however TokenMismatchException means that you are missing a CSRF token in your form.

但是 TokenMismatchException 意味着您的表单中缺少 CSRF 令牌。

You can add this by using <input name="_token" type="hidden" value="{{ csrf_token() }}">

您可以通过使用添加它 <input name="_token" type="hidden" value="{{ csrf_token() }}">

回答by Farid Movsumov

{{ Form::open(array('action' => "WelcomeController@log_in")) }}
...
{{ Form::close() }}

回答by Emmanuel de Carvalho Garcia

You need to set a name to your Routes. Like this:

您需要为您的路线设置一个名称。像这样:

    Route::get('/','WelcomeController@home')->name('welcome.home');
    Route::post('/', array('as' => 'log_in', 'uses' => 'WelcomeController@log_in'))->name('welcome.log_in');
    Route::get('home', 'HomeController@index')->name('home.index');

I just put name on Routes that need this. In my case, to call from tag form at blade template. Like this:

我只是把名字放在需要这个的路线上。在我的情况下,从刀片模板的标签表单中调用。像这样:

<form action="{{ route('home.index') }}" >

Or, You can do this:

或者,您可以这样做:

<form action="/" >

回答by Ashish

Form Post Action :

表单发布操作:

<form method="post" action="{{url('login')}}" accept-charset="UTF-8">

Change your Route : In Routes -> Web.php

改变你的路线:在路线 -> Web.php

Route::post('login','WelcomeController@log_in');

回答by Rio

The following should work.

以下应该工作。

{{  Form::open( array('url' => action('WelcomeController@log_in'), 'files'=>true,'method'=>'post') )  }}

...
{{ Form::close() }}

回答by van_flucht

I wanted to store a post in my application, so I created a controller of posts (PostsController) with the resources included:

我想在我的应用程序中存储一个帖子,所以我创建了一个帖子控制器(PostsController),其中包含以下资源:

php artisan make:controller PostsController --resource

php artisan make:controller PostsController --resource

The controller was created with all the methods needed to do a CRUD app, then I added the following code to the web.phpin the routesfolder :

控制器与所有做一个CRUD应用程序所需的方法创建的,那么我下面的代码添加到web.php路由文件夹:

Route::resource('posts', 'PostsController');

Route::resource('posts', 'PostsController');

I solved the form actionproblem by doing this:

我通过这样做解决了表单操作问题:

  1. I checked my routing list by doing php artisan route:list
  2. I searched for the route name of the store methodin the result table in the terminal and I found it under the nameof posts.store
  3. I added this to the action attributeof my form: action="{{route('posts.store')}}"instead of action="??what to write here??"
  1. 我检查了我的路由列表 php artisan route:list
  2. 我关注的航线名称存储方法在结果表中的终端,我发现它的下名字posts.store
  3. 我将此添加到表单的action 属性中:action="{{route('posts.store')}}"而不是action="??what to write here??"