【问题标题】:Return View in Laravel not displaying proper URLLaravel 中的返回视图未显示正确的 URL
【发布时间】:2014-10-22 04:52:52
【问题描述】:

我现在正在通过使用资源路由进行路由

这是我在路由器中的代码,

Route::resource('item-sales', 'ItemSalesController');

这是我在控制器中的代码

return View::make('item-sales.create')

当我返回视图时,它不会显示我需要的 URL,

URL - item-sales/

我需要/预期的 URL 输出是,

URL - item-sales/create


这是我的控制器

 public function store()
        {
               $id = Input::get('item_id');
                $new_item = Item::find($id);
                $new_qty = Input::get('item_quantity');
                $total = $new_item->item_price * $new_qty;
                Session::put('added-items', [
                    0 => [
                        'item_id'       => $id,
                        'item_name'     => $new_item->item_name,
                        'item_price'    => $new_item->item_price,
                        'item_category'    => $new_item->category,
                        'item_quantity' => Input::get('item_quantity')
                    ]
                ]);
                $array = Session::get('added-items');
                $total = number_format($total, 2, '.', ',');
                return View::make('item-sales.create')
                ->with('items',$array)
                ->with('total',$total);
    }

【问题讨论】:

  • 路由对应于控制器中使用的方法,而不是渲染视图。您正在使用哪种方法/操作?
  • 什么意思?我需要修复 URL 以将其显示为 item-sales/create 而不是 item-sales,它的操作来自控制器 @webNeat
  • 我的意思是,如果您的代码 return View::make('item-sales.create') 是用控制器的 index 方法编写的。 URL 将是/item-sales。检查这个laravel.com/docs/controllers#resource-controllers
  • 是的,它在索引中我怎么能使用 /item-sales/create ?
  • 请将您的控制器代码添加到您的问题中,以便我们为您提供帮助

标签: php url laravel laravel-4 routing


【解决方案1】:

如果您需要自己的自定义方法和单个路由的单个路由,请使用 restful 路由。

在 Routes.php 中:

Route::controller('item-sales', 'ItemSalesController');

在 ItemSalesController.php 中:

public function getCreate() {
   $id = Input::get('item_id');
   $new_item = Item::find($id);
   $new_qty = Input::get('item_quantity');
   $total = $new_item->item_price * $new_qty;
   Session::put('added-items', [
             0 => [
                    'item_id'       => $id,
                    'item_name'     => $new_item->item_name,
                    'item_price'    => $new_item->item_price,
                    'item_category'    => $new_item->category,
                    'item_quantity' => Input::get('item_quantity')
                  ]
   ]);
   $array = Session::get('added-items');
   $total = number_format($total, 2, '.', ',');

   return View::make('item-sales.create')
              ->with('items',$array)
              ->with('total',$total);
}

然后在 Url: route to: item-sales/create

【讨论】:

    【解决方案2】:

    查看文档中的Resource Controller。它的动作使用HTTP 动词映射到方法。因此,例如,要显示用于创建资源的表单,方法应为create,请求方法应为GETURL 应为/item-sales/create

    Verb         Path                         Action    Route Name
    
    GET          /resource                    index     resource.index
    
    // Follow this one
    
    GET          /item-sales/create           create    item-sales.create
    
    POST         /resource                    store     resource.store
    GET          /resource/{resource}         show      resource.show
    GET          /resource/{resource}/edit    edit      resource.edit
    PUT/PATCH    /resource/{resource}         update    resource.update
    DELETE       /resource/{resource}         destroy   resource.destroy
    

    为了确保路由和路由名称在您的命令提示符(终端)中运行 php artisan routes 并准确使用路由名称和 URL。

    store 方法用于保存和使用POST 方法/http 动词,URL 将是/item-sales。要显示用于创建新资源的表单(通过在表单中​​获取用户输入),您应该使用 create 方法,在这种情况下,http 动词将是 GET。所以你的动作应该是这样的:

    public function create()
    {
        // Return the view (empty form)
        // app/views/item-sales/create.blade.php
        return Vire::make('item-sales.create');
    }
    

    要访问/调用此方法,您应该使用item-sales/create 作为URL。那么当你提交表单进行保存时,使用store 方法,所以表单的动作可能类似于route('item-sales.store')

    【讨论】:

    • 我应该声明另一条路线将其自定义为 item-sales/create 吗?
    • 不,你应该使用Laravelartisan命令来创建你的资源控制器,php artisan controller:make YourControllerLaravel会为你创建所有的方法。
    • 我已经这样做了,我有这个控制器和我需要的锅炉,但是当我尝试返回时,它确实很好,但 URL 似乎错误。
    • 您用来显示表单的方法/操作是什么?
    • 它,return View::make('item-sales.create')@WereWolf
    猜你喜欢
    • 2015-05-13
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多