无法通过命名路由传递参数

时间:2013-12-19 18:27:34

标签: php laravel laravel-4

我无法弄清楚我错过了什么。我正在尝试通过命名路由将表单提交给控制器方法,并将其所有输入和image-> id作为参数。我一直在notfoundhttpexception。如果我从路由声明中删除/ {$ id},我会得到控制器操作错误的缺失参数。这是代码:

路线

 Route::post('images/toalbum/{$id}', array('as' => 'imgToAlbum', 'uses' => 'ImagesController@addImageToAlbums'));

routes.php文件

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/


Route::get('/', array('as' => 'home', function()
{
    return View::make('layouts.default');
}));

Route::get('users/login', 'UsersController@getLogin');
Route::get('users/logout', 'UsersController@getLogout');

Route::post('users/login', 'UsersController@postLogin');

Route::resource('users', 'UsersController');

Route::resource('images', 'ImagesController');
//routes related to images
    Route::post('images/toalbum/{$id}', array('as' => 'imgToAlbum', 'uses' => 'ImagesController@addImageToAlbums'));

Route::resource('videos', 'VideosController');

Route::resource('albums', 'AlbumsController');

查看提交表单:

@extends('layouts.default')

    @section('content')
    <?php
    $albumarray = array(null => '');
    ?>

    {{ HTML::image($image['s3Url'], $image['altText']) }}
    <p>
        Title:{{ $image['caption'] }}</br>
        Alt-Text: {{ $image['altText'] }}</br>
        Description: {{ $image['description'] }}</br>
    </p>

    {{ Form::open(array('route' => array('imgToAlbum', $image['id']), 'method' => 'post')); }}


    @foreach ($albums as $album)
        <?php
        array_push ($albumarray, array($album['id'] => $album['caption']));
        ?>
    @endforeach


    {{ Form::label('Add image to album?') }}
    {{ Form::select('album', $albumarray) }}</br>



    {{ Form::submit('Add to Album')}}

    {{Form::close();}}

    <?php
        echo $albums;
    ?>

    @stop

    @section('footer')

    @stop

控制器:

<?php

class ImagesController extends BaseController
{
    protected $image;

    public function __construct(Image $image)
    {
        $this->image = $image;
    }


    // add image to album

    public function addImageToAlbums($id)
    {
        dd($album = Input::all());

        $image = $this->where('id', '=', $id);

        $image->albumId = $album;

        $this->image->save();

        /*return Redirect::route('image.show', $this->image->id)
            ->with('message', 'Your image was added to the album');*/
    }
}

1 个答案:

答案 0 :(得分:1)

也许这将有助于将来某人,所以不是删除这里是答案。从路由声明中删除images / toalbum / {$ id}中的$已解决问题。