简体   繁体   中英

Generating URL::route with parameter in Laravel 4

I am having trouble getting getting my parameter of 'item id' to tack onto the end of my route being generated for my form action url.

I have a page setup to 'update' an existing item. The routes looks something like this:

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',
));

and my ItemController contains these methods:

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!');

    }

}

The last piece of the puzzle is my items.edit view:

@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

The action URL being generated here is wrong:

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

For some reason it is escaping my {id} in the route and not adding on the actual item ID on the end of the route. I have tried a few different ways of doing this and read through the route parameter docs, but I haven't made any progress. I also tried using Laravel's for builder like so:

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

but this did the same thing. I am new to Laravel so this may be a simple problem that I am overlooking, any help is much appreciated.

Use an array in the second parameter.

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

Or the route helper (what I would go with, fits more in view files).

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

It seems that $item->id returns null .

And this is how you do it, when you specify action or route in the Form::open :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM