1
假设以下模型arquitecture:路线模型绑定目标抽象类
namespace MyApp\Models;
abstract class Person{
abstract function getName();
}
然后,我实现它的应用:
namespace MyApp\Models;
class Employ extends Person{
public function getName(){
return "Employ";
}
}
同为用户:
namespace MyApp\Models;
class Employ extends User{
public function getName(){
return "User";
}
}
这里有趣...我使用我自己的解析逻辑int路由绑定上述模型。
Route::bind('person', function($value)
{
// this method returns an instace of Employ OR User
$person = load_person_from_db($value);
return $person;
});
Route::get('test/{person}', function(MyApp\Models\Person $person){
return $person->getName();
});
问题?当我访问网址...
http://example.com/test/1
...以下异常触发:
BindingResolutionException in Container.php line 785:
Target [MyApp\Models\Person] is not instantiable.
呀!我知道,Person
不是实例化,但为什么laravel没有返回的情况下加载由我在这一点? $person = load_person_from_db($value);
。
如果从路由回调中删除类型提示('MyApp \ Models \ Person'),它能正常工作吗? – lukasgeiter 2015-02-07 19:49:44
大声笑...是的,那个炒锅! – manix 2015-02-07 19:52:53
Laravel 5试图尽可能地做依赖注入,这就是为什么你需要省略它;) – cgross 2015-02-07 20:16:56