【问题标题】:Custom function inside controller not found未找到控制器内的自定义功能
【发布时间】:2020-01-07 23:02:30
【问题描述】:

我用resource 创建了一个controller。我在其中创建了一个自定义函数,但是当我在 blade.php 中将它用作 route 时,它说它没有定义。

非常感谢任何有关错误和解释的帮助!

刀片

    <div class="modal fade" id="issueModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <form action="{{route('inventory.deduct')}}" method="post">


        </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary">Save changes</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
            </form>
        </div>
    </div>
</div>

控制器内部的自定义函数

public function deduct(Type $var = null)
{
    dd("test");
}

路线

Route::resource('inventory', 'InventoryController');

【问题讨论】:

  • 你是如何创建路由的?
  • 哦等等我会添加我的路线@TharakaDilshan
  • 我添加了它@TharakaDilshan 请查看编辑
  • 见。资源路由只添加那些特定的路由。如果您定义了任何自定义方法。您需要添加另一个自定义路线。就是这样。

标签: laravel laravel-routing laravel-5.8 laravel-controller


【解决方案1】:
Route::post('/inventory/deduct', 'InventoryController@deduct')->name('inventory.deduct');

将此添加到您在 Web.php 文件中的路由。 Resource 只为控制器创建默认路由,而不是自定义路由。

【讨论】:

  • 那么使用默认资源创建路由,不会影响其中包含自定义资源吗?
  • @kwestionable 不,它没有。无论您制作什么自定义路由,都需要将它们添加到您的 routes/web.php 文件中。
  • 那么关于inventory@deduct 是否指向我的InventoryController?还是我必须制作另一个客户控制器?
  • @AhmadKarimi 我认为应该是InventoryController@deduct
  • @TharakaDilshan,你是对的。我纠正了错误。
【解决方案2】:

资源路由用于indexcreatestoreshoweditupdatedestroy。您的新路线未在资源路线中定义。所以你必须创建一个新的路线来使用它。将此路由添加到 web.php 文件中的资源路由上方

Route::post('inventory/deduct', 'InventoryController@deduct')->name('inventory.deduct');

这将创建你的路由,你可以在你的表单中使用它,你可以让你的控制器做你想做的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 2014-03-01
    • 2017-05-04
    • 1970-01-01
    • 2012-10-29
    相关资源
    最近更新 更多