我正在尝试在提交数据并将数据成功插入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中重定向学习视图,并给出其代码
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
//
public function index(){
return view('plearning');
}
}
请更正我的代码并为我提供帮助。预先感谢
答案 0 :(得分: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']);
});
答案 1 :(得分:1)
您几乎是正确的
public function store(Request $request)
{
//store data in database
test::create(Request::all());
return redirect()->action('HomeController@index');
}
控制器不适用于HTML。您尝试在控制器中添加用于重定向的按钮,不建议这样做。
对于Laravel中的重定向,我们有redirect() helper_function。
如果仍然存在“找不到页面”错误,请确保视图目录中有“ plearning.blade.php”(默认值:resource / views)。