Laravel 5: 路由绑定 returns 空对象
Laravel 5: Route binding returns empty object
我正在尝试使绑定生效(在 youtube 上遵循 tutorial)。在这一点上,我基本上是复制粘贴代码;仍然,它似乎不起作用。
这是 routes.php:
Route::model('song', 'App\Song');
Route::get('songs', 'SongsController@index');
Route::get('songs/{slug}', 'SongsController@show');
Route::get('songs/{slug}/edit', 'SongsController@edit');
Route::patch('songs/{slug}', 'SongsController@update');
Song.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Song extends Eloquent {
protected $fillable = [
'title', 'lyrics'
];
}
和SongsController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Song;
class SongsController extends Controller
{
public function index(Song $song) {
$songs = $song->get();
return view('songs.index', compact('songs'));
}
public function show(Song $song) {
return view('songs.show', compact('song'));
}
public function edit($slug) {
$song = Song::whereSlug($slug)->first();
return view('songs.edit', compact('song'));
}
public function update($slug, Request $request) {
$song = Song::whereSlug($slug)->first();
$song->fill($request->input())->save();
return redirect('songs');
}
}
我故意在 edit
和 update
中留下 $slug
而不是 Song $song
,因为这是问题所在。
如果我理解正确的话,我应该能够在所有 4 个方法中访问 $song 对象(假设第一个参数在所有方法中都是 Song $song
);但是,只有在 index
方法的情况下,我才能从数据库中获取任何内容。其他人只是给我一个(某种)空对象。这是在 show
函数中调用的 dd($song)
(URI 是 http://localhost:8000/songs/boyfriend
或 http://localhost:8000/songs/1
- 无论使用 Route::bind
而不是 [=29,两者都会导致相同的输出=],如下所述):
Song {#144 ▼
#fillable: array:2 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
}
数据库中的条目存在;修补匠:
>>> App\Song::whereSlug('boyfriend')->first();
=> <App\Song #000000005ba18d590000000003230878> {
id: 1,
title: "Boyfriend",
(...)
edit
和 update
方法完美无缺(除非我尝试删除 $slug
并将 Song $song
作为第一个参数)。
我已经尝试了 updating the $compiledPath、clear-compiled
、cache:clear
,重新启动了十亿次;我也尝试用
替换 Route::model
行
Route::bind('song', function($slug) {
return App\Song::whereSlug($slug)->first();
});
在 routes.php
文件中(使用 $router->bind
语法也是一样)。我正在使用 XAMPP 和 PHP 5.6.8,项目通过 php artisan serve
开始,php artisan --version
是 Laravel Framework version 5.1.2 (LTS)
。
我在 Route::bind 中尝试了 dd($slug) - 它什么都不做(就好像它不存在一样),与 findOrFail
和 firstOrFail
一样;但是,将 blah!
放在 return
之前会导致 FatalErrorException
,因此该函数似乎确实被调用了。我在这里错过了什么?
这是一个错误吗?这应该正常工作吗?
您似乎绑定到 song
,但在您的 routes.php
中,占位符为 slug
。您需要将其更改为 song
Route::get('songs', 'SongsController@index');
Route::get('songs/{song}', 'SongsController@show');
Route::get('songs/{song}/edit', 'SongsController@edit');
Route::patch('songs/{song}', 'SongsController@update');
我正在尝试使绑定生效(在 youtube 上遵循 tutorial)。在这一点上,我基本上是复制粘贴代码;仍然,它似乎不起作用。
这是 routes.php:
Route::model('song', 'App\Song');
Route::get('songs', 'SongsController@index');
Route::get('songs/{slug}', 'SongsController@show');
Route::get('songs/{slug}/edit', 'SongsController@edit');
Route::patch('songs/{slug}', 'SongsController@update');
Song.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Song extends Eloquent {
protected $fillable = [
'title', 'lyrics'
];
}
和SongsController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Song;
class SongsController extends Controller
{
public function index(Song $song) {
$songs = $song->get();
return view('songs.index', compact('songs'));
}
public function show(Song $song) {
return view('songs.show', compact('song'));
}
public function edit($slug) {
$song = Song::whereSlug($slug)->first();
return view('songs.edit', compact('song'));
}
public function update($slug, Request $request) {
$song = Song::whereSlug($slug)->first();
$song->fill($request->input())->save();
return redirect('songs');
}
}
我故意在 edit
和 update
中留下 $slug
而不是 Song $song
,因为这是问题所在。
如果我理解正确的话,我应该能够在所有 4 个方法中访问 $song 对象(假设第一个参数在所有方法中都是 Song $song
);但是,只有在 index
方法的情况下,我才能从数据库中获取任何内容。其他人只是给我一个(某种)空对象。这是在 show
函数中调用的 dd($song)
(URI 是 http://localhost:8000/songs/boyfriend
或 http://localhost:8000/songs/1
- 无论使用 Route::bind
而不是 [=29,两者都会导致相同的输出=],如下所述):
Song {#144 ▼
#fillable: array:2 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
}
数据库中的条目存在;修补匠:
>>> App\Song::whereSlug('boyfriend')->first();
=> <App\Song #000000005ba18d590000000003230878> {
id: 1,
title: "Boyfriend",
(...)
edit
和 update
方法完美无缺(除非我尝试删除 $slug
并将 Song $song
作为第一个参数)。
我已经尝试了 updating the $compiledPath、clear-compiled
、cache:clear
,重新启动了十亿次;我也尝试用
Route::model
行
Route::bind('song', function($slug) {
return App\Song::whereSlug($slug)->first();
});
在 routes.php
文件中(使用 $router->bind
语法也是一样)。我正在使用 XAMPP 和 PHP 5.6.8,项目通过 php artisan serve
开始,php artisan --version
是 Laravel Framework version 5.1.2 (LTS)
。
我在 Route::bind 中尝试了 dd($slug) - 它什么都不做(就好像它不存在一样),与 findOrFail
和 firstOrFail
一样;但是,将 blah!
放在 return
之前会导致 FatalErrorException
,因此该函数似乎确实被调用了。我在这里错过了什么?
这是一个错误吗?这应该正常工作吗?
您似乎绑定到 song
,但在您的 routes.php
中,占位符为 slug
。您需要将其更改为 song
Route::get('songs', 'SongsController@index');
Route::get('songs/{song}', 'SongsController@show');
Route::get('songs/{song}/edit', 'SongsController@edit');
Route::patch('songs/{song}', 'SongsController@update');