自定义路由在 Laravel 5.5 中不起作用
Custom Routing is not working in Laravel 5.5
我需要从 blade 页面向控制器操作提交表单。它目前无法正常工作。我有blade查看页面如下
index.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<form action="{{route('api.api_submit')}}" method="post">
<div class="form-group"> <label for="country">Country:</label> <input type="text"
class="form-control" name="country" />
</div>
<div class="form-group"> <label for="job_title">Job Title:</label> <input type="text"
class="form-control" name="job_title" />
</div>
<button type="submit" class="btn btn-primary-outline">Submit</button>
</form>
</div>
</div>
@endsection
控制器如下:
ApiController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Contact;
class ApiController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('api.index');
}
/**
* 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 a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function api_submit(Request $request)
{
$contacts = Contact::all();
return view('api.show', compact('contacts'));
}
/**
* 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)
{
//
}
}
在 routes/web.php 我有
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
/**
* API routes
*/
Route::get('/api', function () {
return view('api/index');
});
Route::get('/api/v1', 'ApiController@index');
Route::post('/api/api_submit', 'ApiController@api_submit');
但是每当我调用 http://localhost:8000/api
时它都会抛出错误
Route [api.api_submit] not defined
。为什么会这样?是否需要做更多工作才能使该特定路线正常工作?
我也试过给资源路由Route::resource('api', 'ApiController');
。也没用。
附上下面的截图。
您必须为路线命名。
Route::post('/api/api_submit', 'ApiController@api_submit')->name('api.api_submit');
我需要从 blade 页面向控制器操作提交表单。它目前无法正常工作。我有blade查看页面如下
index.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<form action="{{route('api.api_submit')}}" method="post">
<div class="form-group"> <label for="country">Country:</label> <input type="text"
class="form-control" name="country" />
</div>
<div class="form-group"> <label for="job_title">Job Title:</label> <input type="text"
class="form-control" name="job_title" />
</div>
<button type="submit" class="btn btn-primary-outline">Submit</button>
</form>
</div>
</div>
@endsection
控制器如下:
ApiController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Contact;
class ApiController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('api.index');
}
/**
* 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 a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function api_submit(Request $request)
{
$contacts = Contact::all();
return view('api.show', compact('contacts'));
}
/**
* 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)
{
//
}
}
在 routes/web.php 我有
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
/**
* API routes
*/
Route::get('/api', function () {
return view('api/index');
});
Route::get('/api/v1', 'ApiController@index');
Route::post('/api/api_submit', 'ApiController@api_submit');
但是每当我调用 http://localhost:8000/api
时它都会抛出错误
Route [api.api_submit] not defined
。为什么会这样?是否需要做更多工作才能使该特定路线正常工作?
我也试过给资源路由Route::resource('api', 'ApiController');
。也没用。
附上下面的截图。
您必须为路线命名。
Route::post('/api/api_submit', 'ApiController@api_submit')->name('api.api_submit');