【问题标题】:After posting a form going to a new view Laravel 5将表单发布到新视图 Laravel 5 后
【发布时间】:2016-05-04 02:01:01
【问题描述】:

我在将所有数据发布到数据库后尝试重定向到另一个页面。我让它发布数据而无需转到另一个视图,但现在我添加了它也不会执行的视图,我收到此错误:

NotFoundHttpException in RouteCollection.php line 161:

这是我的 routes.php 文件

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/


Route::Get('/', function()
{
    return View::Make('welcome');
});

Route::Get('registration', function()
{
    return View::make('registration');
});


Route::post('registration', function()
{
    $user = new \App\User;
    $user->UserName = Input::get('UserName');
    $user->Password = Hash::make(Input::get('password'));
    $user->FirstName = Input::get('FirstName');
    $user->LastName = Input::get('LastName');
    $user->Gender = Input::get('Gender');
    $user->Email = Input::get('Email');
    $user->Q1 = Input::get('Q1');
    $user->Q2 = Input::get('Q2');
    $user->Q3 = Input::get('Q3');
    $user->save();
    return View::make('welcomepage');
});

任何建议都会很棒。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    以下是修改后的route.php 示例。在将用户保存到数据库后,它将用户重定向到名为registrationsuccess 的页面。这就是你想要的吗? 注意:您需要在resources/views/registrationsuccess.blade.php下有一个模板,其中包含您注册成功后要显示的内容。

    // Welcome page
    Route::get('/', function()
    {
        return View::Make('welcome');
    });
    
    // Registration GET, with empty form
    Route::get('registration', function()
    {
        return View::make('registration');
    });
    
    // Registration POST, with form content
    Route::post('registration', function()
    {
        $user = new \App\User;
        $user->UserName = Input::get('UserName');
        $user->Password = Hash::make(Input::get('password'));
        $user->FirstName = Input::get('FirstName');
        $user->LastName = Input::get('LastName');
        $user->Gender = Input::get('Gender');
        $user->Email = Input::get('Email');
        $user->Q1 = Input::get('Q1');
        $user->Q2 = Input::get('Q2');
        $user->Q3 = Input::get('Q3');
        $user->save();
        return redirect('registrationsuccess');
    });
    
    // Success page after registration
    Route::get('welcomepage', function()
    {
        return view('welcomepage');
    });
    

    【讨论】:

    • 这还是不行!我真的无法弄清楚问题出在哪里。我将registrationsuccess改为welcomepage,还是没有成功
    • 好的,我在我的代码示例中更改了它。在您的route.php 中是不是这样,并且您仍然遇到相同的NotFoundHttpException in RouteCollection.php line 161: 错误?你能发布一个更大的堆栈跟踪吗?
    • 感谢您的帮助,我设法清除缓存并再次尝试,它成功了!
    • @Jeigh 很高兴听到这个消息!请将帖子标记为解决方案。
    【解决方案2】:

    在你的/ 路由中,你的 Make 方法应该是小写字母..

    Route::get('/', function()
    {
        return View::make('welcome');
    });
    

    【讨论】:

    • Route::Get() 也可以这样说,而不是 Route::get();
    • 当我改变它时,这似乎没有任何区别
    • “欢迎”是我试图指向“欢迎页面”的标准路线
    • 您是否添加了“欢迎页面”模板?路由 / 返回 'welcome' 模板,路由 post:registration 返回 'welcomepage' 模板。
    • @codedge 我猜你的意思是在我的公共文件夹中,那么是的。如果我自己制作路线,我可以直接到达它,但不能在表单提交之后。
    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 2011-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2017-01-17
    相关资源
    最近更新 更多