【问题标题】:Custom Routing is not working in Laravel 5.5自定义路由在 Laravel 5.5 中不起作用
【发布时间】:2019-12-10 08:44:00
【问题描述】:

我需要从刀片页面提交表单到控制器操作。到目前为止,它无法正常工作。我的刀片视图页面如下

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

和Controller如下:

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 时,它都会给我带来错误

路由 [api.api_submit] 未定义

。为什么会这样?是否需要做更多的工作才能使该特定路线发挥作用?

我也试过给资源路由Route::resource('api', 'ApiController');。它也没有工作。

附上下面的截图。

【问题讨论】:

    标签: laravel routes laravel-5.5 laravel-blade


    【解决方案1】:

    您必须命名您的路线。

    Route::post('/api/api_submit', 'ApiController@api_submit')->name('api.api_submit');
    

    【讨论】:

    • 是的,我刚刚从另一个问题中理解了:))。实际上,其他控制器发布表单操作没有命名路由。只有资源路线。这就是我困惑的原因。无论如何谢谢>
    猜你喜欢
    • 2018-03-17
    • 2018-05-26
    • 2015-03-01
    • 1970-01-01
    • 2017-11-09
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多