假设我有两个实现契约(UserRepositoryInterface、RoleRepositoryInterface)的存储库(UserRepository、RoleRepository)。
我想做的是将 RoleRepository 注入(inject)到 UserRepository 的构造函数方法中,我一直在努力让它工作,但我收到了这个错误:
Target [App\Contracts\UserRepositoryInterface] is not instantiable while building [App\Http\Controllers\UserController].
这是我到目前为止得到的,
Composer .json
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"tymon/jwt-auth": "^0.5.9",
"zizaco/entrust": "dev-laravel-5"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "^0.9.4",
"phpunit/phpunit": "~4.0",
"symfony/css-selector": "2.8.*|3.0.*",
"symfony/dom-crawler": "2.8.*|3.0.*"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\": "app/Models/",
"App\\Contracts\\": "app/Contracts/",
"App\\Repositories\\": "app/Repositories/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"tests/MockData.php"
]
}
配置/app.php
'providers' => [
/*
* Laravel Framework Service Providers...
*/
...
/**
* Third-party Service Providers
*/
...
/*
* Application Service Providers...
*/
...
/**
* Custom Service Providers...
*/
App\Providers\RepositoryServiceProvider::class,
app/Providers/RepositoryServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
public function register()
{
$models = [
'Role',
'User',
];
foreach ($models as $model) {
$this->app->bind(
"App\Contracts\\{$model}Interface",
"App\Repositories\\{$model}Repository"
);
}
}
}
应用程序/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Contracts\UserRepositoryInterface as UserRepository;
class UserController extends Controller
{
/**
* @var UserRepository
*/
private $repo;
/**
* Class constructor
*
* @param UserRepository $repo
*/
public function __construct(
UserRepository $repo
) {
$this->repo = $repo;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$data = $request->all();
$user = $this->repo->store($data);
return response()->json($user, 201);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
*
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, $id)
{
$data = $request->all();
$user = $this->repo->store($data, $id);
return response()->json($data, 200);
}
app/Contracts/RoleRepositoryInterface.php
<?php
namespace App\Contracts;
interface RoleRepositoryInterface
{
/**
* Attach role to user
*
* @param static $user
* @param string $roleName
*
* @return static
*/
public function role($user, $roleName);
}
app/Contracts/UserRepositoryInterface.php
<?php
namespace App\Contracts;
interface UserRepositoryInterface
{
/**
* Create new or update an existing user
*
* @param array $data
* @param int $id
*
* @return bool|int|array
*/
public function store(array $data, $id = null);
}
app/Repositories/RoleRepository.php
<?php
namespace App\Repositories;
use InvalidArgumentException;
use App\Contracts\RoleRepositoryInterface;
use App\Models\Role;
class RoleRepository extends Role implements RoleRepositoryInterface
{
/**
* Attach a role to user
*
* @param static $user
* @param string $roleName
*
* @return static
*
* @throws \InvalidArgumentException
*/
public function attachRole($user, $roleName)
{
if (!$user instanceof App\Models\User::class) {
throw new InvalidArgumentException("{$user} must be an instance of App\Models\User");
}
// find role
$role = $this->where('name', $roleName)->first();
// attach role to user
$user->attachRole($role);
return $user;
}
}
应用程序/Repositories/UserRepository.php
<?php
namespace App\Repositories;
use App\Contracts\RoleRepositoryInterface as RoleRepository;
use App\Contracts\UserRepositoryInterface;
use App\Models\User;
class UserRepository extends User implements UserRepositoryInterface
{
/**
* @var RoleRepository
*/
private $roleRepo;
/**
* Class constructor
*
* @param RoleRepository $roleRepo
*/
public function __construct(RoleRepository $roleRepo)
{
$this->roleRepo = $roleRepo;
}
/**
* Create new or update an existing user
*
* @param array $data
* @param int $id
*
* @return bool|int|static
*/
public function store(array $data, $id = null)
{
if (is_null($id)) {
$user = $this->create($data);
$userWithRole = $this->roleRepo->attachRole($user, $data['role']);
return $user;
} else {
return $this->where('id', $id)
->update($data);
}
}
}
非常感谢任何答案...
最佳答案
我认为您的代码有错误,特别是在 RepositoryServiceProvider 中。应该是:
foreach ($models as $model) {
$this->app->bind(
"App\Contracts\\{$model}RepositoryInterface",
"App\Repositories\\{$model}Repository"
);
}
注意:错误在串联,你得到了"App\Contracts\\{$model}Interface",但它应该是"App\Contracts\\{$model}RepositoryInterface"
关于php - 如何在 Laravel 5 (5.2) 中的另一个存储库类中注入(inject)一个存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36394454/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>