【问题标题】:Generating URL::route with parameter in Laravel 4在 Laravel 4 中生成带有参数的 URL::route
【发布时间】:2014-11-13 05:33:41
【问题描述】:

我无法让我的“项目 ID”参数附加到为我的表单操作 url 生成的路由的末尾。

我有一个页面设置来“更新”现有项目。路线看起来像这样:

Route::get('/item/edit/{id}', array(
    'as' => 'item-edit',
    'uses' => 'ItemController@getEditItem',
));



Route::post('/item/edit/{id}', array(
    'as' => 'item-edit-post',
    'uses' => 'ItemController@postEditItem',
));

我的 ItemController 包含这些方法:

public function getEditItem($id) {

    $states = State::where('user_id', '=', Auth::user()->id)->get();

    $types = Type::where('user_id', '=', Auth::user()->id)->get();

    $item = Item::where('user_id', '=', Auth::user()->id)
            ->where('id', '=', $id)
            ->first();

    return View::make('items.edit')
            ->with('item', $item)
            ->with('states', $states)
            ->with('types', $types);

}

public function postEditItem($id) {

    // Validate input for Item changes
    $validator = Validator::make(Input::all(),
        array(
            'label'    => 'required|min:3|max:128|unique:items,label,null,id,user_id,' . Auth::user()->id,
            'type_id'  => 'required|integer',
            'state_id' => 'required|integer',
        )
    );

    if( $validator->fails() ) {

        return Redirect::route('item-edit')
               ->withErrors($validator)
               ->withInput();

    } else {

        $item               = Item::find($id);
        $item->label        = Input::get('label');
        $item->type_id      = Input::get('type_id');
        $item->state_id     = Input::get('state_id');
        $item->save();

        return Redirect::route('item-create')
               ->with('global', 'Your new item has been edited successfully!');

    }

}

最后一块拼图是我的items.edit 视图:

@extends('layout.main')

@section('content')

    <form action="{{ URL::route('item-edit-post', $item->id) }}" method="post" class="form-horizontal form-bordered" autocomplete="off">
        <!-- some inputs and such -->
    </form>

@stop

此处生成的操作 URL 错误:

<form method="POST" action="http://manageitems.com/item/edit/%7Bid%7D" accept-charset="UTF-8" hello="hello" class="form-horizontal form-bordered">

由于某种原因,它在路由中转义了我的{id},而不是在路由末尾添加实际项目 ID。我尝试了几种不同的方法,并通读了路由参数文档,但我没有取得任何进展。我也尝试过像这样使用 Laravel 的构建器:

{{ Form::open(array('action' => 'ItemController@postEditItem', $item->id, 'class'=>'form-horizontal form-bordered')) }}

但这做了同样的事情。我是 Laravel 的新手,所以这可能是一个我忽略的简单问题,非常感谢任何帮助。

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    在第二个参数中使用数组。

    URL::route('item-edit-post', ['id' => $item->id])
    

    或者 route 助手(我会使用,更适合视图文件)。

    route('item-edit-post', ['id' => $item->id])
    

    【讨论】:

    • 它不会改变任何东西,参数无论如何都会转换为数组。
    • 我将我的 URL::route 更新为 {{ URL::route('item-edit-post', ['id' =&gt; $item-&gt;id]) }},当我查看页面时,我可以看到操作 url 现在是正确的,但是在我提交表单后,我被带到 /item/edit/%7Bid%7D 和 Laravel抱怨Trying to get property of non-object@JarekTkaczyk
    • 我猜,根据你的代码,你的验证失败了,你被重定向到Redirect::route('item-edit') - 你没有提供id,所以它会抛出错误。只是一个猜测 - 错误说明它发生在哪里,所以检查一下。
    【解决方案2】:

    似乎$item-&gt;id 返回null

    当您在Form::open 中指定actionroute 时,您就是这样做的:

    Form::open(['route' => ['some.route', $param]]);
    
    Form::open(['action' => ['controller@action', $param]]);
    

    【讨论】:

      猜你喜欢
      • 2015-07-13
      • 2014-06-08
      • 2020-03-12
      • 1970-01-01
      • 2016-05-23
      • 2013-06-09
      • 2023-03-14
      • 2015-01-14
      • 2015-01-09
      相关资源
      最近更新 更多