【问题标题】:Page is not redirecting in Laravel Controller after data insertion数据插入后页面未在 Laravel 控制器中重定向
【发布时间】:2018-09-30 21:33:42
【问题描述】:

在提交数据并在 laravel 中成功插入数据后,我尝试将页面重定向到表单或主页,但错误显示找不到该页面。请帮我。 我的控制器代码是 testing.php

<?php

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

use App\test;

class testing extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('create');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //store data in database
        test::create(Request::all());
       echo "data enserted Successfully";
       ?><a href="<?php return redirect()->action('HomeController@index');?>">Go Back Home</a><?php  

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

并给出了create.blade.php形式的视图文件

    @extends('master')

@section('content') 

<div class="container"><br>
        <h1 class="text-success text-center">Student Registration Form</h1><br>
        <div class="col-md-offset-3 col-md-6 m-auto d-block">
            <form action="save" method="post" onsubmit="return validation()">

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

                <div class="form-group">
                    <label>Student Name: </label>
                    <input type="text" name="sname" id="sname" class="form-control">
                    <span id="studenterror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Father Name: </label>
                    <input type="text" name="fname" id="fname" class="form-control">
                    <span id="fnameerror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Graduation Year: </label>
                    <input type="date" name="gyear" id="gyear" class="form-control">
                    <span id="gyearerror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Phone: </label>
                    <input type="number" name="phone" id="phone" class="form-control">
                    <span id="phoneerror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Email: </label>
                    <input type="email" name="email" id="email" class="form-control">
                    <span id="emailerror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Postal Address: </label>
                    <input type="text" name="paddress" id="paddress" class="form-control">
                    <span id="addresserror" class="text-danger font-weight-bold"></span>
                </div>

                <div class="form-group">
                    <label>Program: </label>
                    <select class="form-control" name="program">
                        <option  value="Nill">Select Program</option>
                        <option  value="msc">MSc</option>
                        <option  value="bs">BS</option>
                        <option  value="mphil">MPhil</option>
                        <option  value="phd">PHD</option>
                    </select>
                </div>

                <div>
                    <label>Job</label><br>
                    <div class="form-control radio-inline">
                    <div class="col-md-4">
                        <label>
                            <input type="radio" name="job" value="yes" data-toggle="collapse" data-target="#org">Yes</label>
                        <label>
                            <input type="radio" name="job" value="no" data-toggle="collapse" data-target="#">No      </label>
                        </div>
                    </div>
                    <br>


  <div id="org" class="collapse">
    <div class="form-group">
                    <label>Organization: </label>
                    <select class="form-control" name="org">
                        <option  value="Nill">Select Organization</option>
                        <option value="Higher_Education">Higher Education</option>
                        <option  value="Software_House">Software House</option>
                        <option  value="Hardware_industry">Hardware Industry</option>
                        <option  value="other">Other</option>
                    </select>
                    </div>
                <label>Position</label>
                <input type="text" name="position" class="form-control" id="position">

                </div>
  </div><br>
  <div>
    <input type="submit" name="Submit" class="btn btn-lg col-md-offset-3 col-md-6 m-auto d-block">
  </div>


            </form>

        </div>
    </div>
    <h4 align="center">---------------------------------------------------<br>
    First Task of Project Learning</h4>

<br>
<a href="{{url('/plearning')}}" class="btn btn-primary">Back to Home</a>

@endsection

我想在 HomeController@index 中重定向 plearning 视图并给出它的代码

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    //
    public function index(){
        return view('plearning');
    }


}

请更正我的代码并帮助我。在此先感谢

【问题讨论】:

  • 所以如果你的存储方法调用正确,你想重定向吗?
  • 是的,我想用成功消息重定向回来

标签: php laravel


【解决方案1】:

要在发布后重定向,您应该将 store 方法更新为以下内容:

public function store(Request $request)
{
    //store data in database
    Test::create($request->all());

    return redirect()->route('test.index');
}

如果这个 test.index 不起作用,我不确定你是如何定义你的路由的,将你的路由发布到你的问题。

您的测试路线应如下所示:

Route::group(['prefix' => 'test', 'as' => 'test.'], function () {
    Route::get('/', ['as' => 'index', 'uses' => 'TestController@index']);
    Route::get('create', ['as' => 'create', 'uses' => 'TestController@create']);
});

【讨论】:

    【解决方案2】:

    你几乎是对的

    public function store(Request $request)
    {
       //store data in database
       test::create(Request::all());
       return redirect()->action('HomeController@index'); 
    }
    

    控制器不适用于 HTML。您尝试在控制器中添加一个用于重定向的 Button,不建议这样做。

    对于 Laravel 中的重定向,我们有 redirect()helper_function。

    如果仍然出现“找不到页面”错误,请确保视图目录中有“plearning.blade.php”(默认:resource/views)。

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 2021-07-08
      相关资源
      最近更新 更多