【问题标题】:Can't pass a parameter through a named route无法通过命名路由传递参数
【发布时间】:2014-01-08 10:53:15
【问题描述】:

我无法弄清楚我错过了什么。我正在尝试通过命名路由将表单提交给控制器方法,并将其所有输入和 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');*/
    }
}

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-10-12
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2019-09-06
      相关资源
      最近更新 更多