简体   繁体   English

RouteCollection.php Laravel中的MethodNotAllowedHttpException

[英]MethodNotAllowedHttpException in RouteCollection.php Laravel

I have problem to save name and email from user1 in table user1s that I have made . 我在将user1的名称和电子邮件保存到我创建的表user1s中时遇到问题。

When I enter them in textareas using html form in Laravel with route::post and function store it is not working. 当我在Laravel中使用html形式的route::post和function存储在textareas中输入它们时,它不起作用。 When I enter text and hit the button Register it outputs the following error: 当我输入文字并点击“注册”按钮时,将输出以下错误:

MethodNotAllowedHttpException in RouteCollection.php line RouteCollection.php行中的MethodNotAllowedHttpException

You will see that I use the HTML form and that I have tried to add <input ....> into my form. 您将看到我使用HTML表单,并且尝试将<input ....>添加到我的表单中。


Here are my files: 这是我的文件:

route.php route.php

<?php

Route::get('/','PageController@home');
Route::post('/','User1Controller@store');

Route::get('about','PageController@about');

welcome.blade.php welcome.blade.php

I'm not sure about the action . 我不确定该action After putting user1 inf into table, it should be redirected to a "Thank you" page (I have a thankyou.blade.php ) , maybe that is the problem 将user1 inf放入表后,应将其重定向到“谢谢”页面 (我有Thankyou.blade.php ),也许就是问题所在

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

                <ul class="list-group" >


                    <li  >
                    NAme
                    <div class="form-group" title="email" >
                        <textarea name="name" class="form-control" >

                        </textarea>
                    </div>
                    </li  >

                    <li>Email
                    <div class="form-group" > 
                        <textarea name="email"  class="form-control" >

                        </textarea>
                    </div>
                    </li>

                    <li  >
                        <div class="form-group" >
                        <button class="btn btn-primary">Register</button>
                        </div>
                    </li>

                </ul>
            </form>

migration for user1 为user1迁移

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateNotesTable extends Migration
{

    public function up()
    {
        Schema::create('notes', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('card_id')->unsigned();
            $table->text('body');            
            $table->timestamps();
        });
    }


    public function down()
    {
        Schema::drop('notes');
    }
}

user1controller.php user1controller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User1;

class User1Controller extends Controller
{

    public function store(Request $request)
        {

            $user= new User1;
            $user->name = $request->name;
            $user->email =  $request->email;
            $user->save();

            return view('thankyou');

        }

}

pagecontroller.php pagecontroller.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

use App\User1;

class PageController extends Controller
{
    public function home()
    {
            $user1s= User1::all();
    return view('welcome',compact('user1s'));
    }

    public function about()
    {
            return view('pages.about'); 
    }

}

Your form is basically a registration form. 您的表格基本上是注册表格。 I would recommend using a more meaningful name for the end point. 我建议为终点使用更有意义的名称。 The post route can be something like, 发布路线可以是这样的,

Route::post('/register','User1Controller@store');

Now the form action can be, 现在可以执行表单操作了

action="/register"

I corrected the typo.Thanks! 我纠正了错字。谢谢!

I have also changed this Route::post('/','User1Controller@store'); 我也更改了此Route :: post('/','User1Controller @ store'); and action=" " . 和action =“”。

It works ,the only thing right now that is not good is that I should redirect to a page "Thank you" not go to anther view on the exact same page. 它有效,唯一不好的是我应该重定向到页面“谢谢”,而不要转到完全相同页面上的另一个视图。

Because It makes a mess in the database when I reload the home page. 因为当我重新加载主页时,它会使数据库混乱。 I'll try that and tell if it works. 我会尝试一下,然后告诉它是否有效。

Thank you people for the help! 谢谢大家的帮助! :) :)

Things solved,this works. 事情解决了,行得通。 I will add the code that i have added so the other can find it! 我将添加已添加的代码,以便其他人可以找到它!

Firs of all : I haven't figured why ,but action="dir1/dir3" for me didn't work! 所有人:我还没弄清楚为什么,但是对我来说action =“ dir1 / dir3”无效!

Here are the added things! 这是添加的东西!

 routes.php Route::get('thankyou','PageController@thankyou'); ***PageController.php*** public function thankyou() { return view('thankyou'); } *****User1Controller.php***** public function store(Request $request) { $user= new User1; $user->name = $request->name; $user->email = $request->email; $user->save(); return redirect('/thankyou'); } 

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

相关问题 RouteCollection.php中的MethodNotAllowedHttpException-Laravel - MethodNotAllowedHttpException in RouteCollection.php - laravel Laravel 5中的RouteCollection.php中的MethodNotAllowedHttpException - MethodNotAllowedHttpException in RouteCollection.php in Laravel 5 RouteCollection.php 第 219 行中的 Laravel 5 MethodNotAllowedHttpException - Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 219 Laravel 5:RouteCollection.php第219行中的MethodNotAllowedHttpException - Laravel 5: MethodNotAllowedHttpException in RouteCollection.php line 219 Laravel。 RouteCollection.php中的MethodNotAllowedHttpException - Laravel. MethodNotAllowedHttpException in RouteCollection.php RouteCollection.php第218行中的Laravel MethodNotAllowedHttpException: - Laravel MethodNotAllowedHttpException in RouteCollection.php line 218: Laravel 5.1中RouteCollection.php中的MethodNotAllowedHttpException - MethodNotAllowedHttpException in RouteCollection.php in laravel 5.1 MethodNotAllowedHttpException在laravel 5中的RouteCollection.php第207行 - MethodNotAllowedHttpException RouteCollection.php line 207 in laravel 5 Laravel-RouteCollection.php第251行中的MethodNotAllowedHttpException - Laravel - MethodNotAllowedHttpException in RouteCollection.php line 251 RouteCollection.php第201行中的Laravel 5 MethodNotAllowedHttpException: - Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 201:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM