【问题标题】:Error in routing and function in controller控制器中的路由和功能错误
【发布时间】:2014-04-11 21:08:31
【问题描述】:

在 app/routes.php 文件中添加“Route::resource('cat', 'CatController');”

这是一个 CatController 代码:

<?php

class CatController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
         return 'This is cat controller index.';

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
        return 'This is a form to create new cat !';
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
        return 'Show the cat number:' . $id;
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }

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

}

当我调用 url:http://www.domain.com/laravel/public/cat/edit,它返回:

显示猫号:编辑

为什么会返回显示函数内容?!

【问题讨论】:

    标签: laravel-4


    【解决方案1】:

    edit($id) 的 URL 模式是 /resource/{id}/edit - 您只是使用 /resource/edit,它被解释为 /resource/{id},指向 show($id) 方法。

    【讨论】:

    • 当我使用 /public/cat/edit/4564 时,它得到错误 Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
    • 错误的顺序。再次阅读我写的内容(也许还有文档)。
    • 嗯,它可以工作,但是当有 2 个带参数的函数时,它返回错误
    猜你喜欢
    • 2013-09-30
    • 2015-02-07
    • 2011-02-18
    • 1970-01-01
    • 2021-09-06
    • 2013-05-06
    • 2018-01-09
    • 2020-02-27
    • 2015-10-27
    相关资源
    最近更新 更多