[英]repository pattern in laravel doesn't seem to work
App::bind('App\Http\Repositories\languageRepository',
function( $app, array $parameters)
{
return new App\Http\Repositories\languageRepository($parameters[0]);
} );
Route::get('/test/{id}', 'testController@getme');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Repositories\languageRepository;
class test extends Controller
{
//
protected $language;
public function __construct(languageRepository $rep){
$this->language = $rep;
}
public function getme(){
$this->language->getMe();
}
}
When user accesses the route /test/5 for example, it goes to test Controller.例如,当用户访问路由 /test/5 时,它会转到测试控制器。 what I'd like to do is that it should automatically pass my route parameter to App:bind function and automatically create languageRepository class with the constructor value passed as my route paramter.
我想要做的是,它应该自动将我的路由参数传递给 App:bind 函数,并使用作为我的路由参数传递的构造函数值自动创建 languageRepository 类。 what happens is the code actually tells me $parameters[0] is undefined offset.
发生的事情是代码实际上告诉我 $parameters[0] 是未定义的偏移量。 why is that?
这是为什么? I've tried App::make but then how do I pass the parameter from route to App::make?
我已经尝试过 App::make,但是如何将参数从路由传递到 App::make?
You can accomplish this using the container's request instance, for query parameters:您可以使用容器的请求实例来完成此操作,以获取查询参数:
App::bind('App\Http\Repositories\languageRepository',function($app)
{
$request = $app['request'];
$parameters = $request->all();
return new App\Http\Repositories\languageRepository($parameters[0]);
});
You can accomplish this using the container's request instance, for a route parameter:您可以使用容器的请求实例来完成此操作,作为路由参数:
App::bind('App\Http\Repositories\languageRepository',function($app)
{
$request = $app['request'];
$segment = $request->segment(1);
return new App\Http\Repositories\languageRepository($segment);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.