Php 无法通过命名路由传递参数
我不知道我错过了什么。我试图让一个表单通过一个命名的路由提交给一个控制器方法,并将其所有输入和image->id作为参数。我一直收到一个notfoundhttpexception。如果我从路由声明中删除/{$id},我会得到一个控制器操作错误的缺失参数。代码如下: 路线Php 无法通过命名路由传递参数,php,laravel,laravel-4,Php,Laravel,Laravel 4,我不知道我错过了什么。我试图让一个表单通过一个命名的路由提交给一个控制器方法,并将其所有输入和image->id作为参数。我一直收到一个notfoundhttpexception。如果我从路由声明中删除/{$id},我会得到一个控制器操作错误的缺失参数。代码如下: 路线 Route::post('images/toalbum/{$id}', array('as' => 'imgToAlbum', 'uses' => 'ImagesController@addImageToAlbum
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');*/
}
}
也许这会对将来的人有所帮助,所以这里不是删除,而是答案。在路由声明中删除images/toalbum/{$id}中的$解决了这个问题