duanhuang2804 2016-06-05 04:44
浏览 39
已采纳

在Laravel 5.2中实现合同时出错

I am following this link to implement it

I did below steps to implement the Contract in my existing class.

Below is the class where I will write some logic also before sending it to controller

namespace App\Classes\BusinessLogic\Role;
use App\Classes\DatabaseLayer\Role\RoleDb;
use App\Classes\Contract\Role\IRole;

class RoleBL implements IRole {

    public function All() {
        return (new RoleDb())->All();
    }
}

Database Function

namespace App\Classes\DatabaseLayer\Role; 

class RoleDb {
    public function All() {
        $Roles = \App\Models\Role\RoleModel
                ::all();
        return $Roles;
    }
}

Interface

namespace App\Classes\Contract\Role;

interface IRole {
    public function All();
}

Service Provider class

namespace App\Providers\Role;

class RoleServiceProvider extends \Illuminate\Support\ServiceProvider {

    public function register()
    {
        $this->app->bind('App\Classes\Contract\Role\IRole', function($app){
            return new \App\Classes\BusinessLogic\Role\RoleBL($app['HttpClient']);
        });
    }
}

Finally in config/app.php in provider wrote below line.

App\Providers\Role\RoleServiceProvider::class

Controller - Constructor

protected $roles;
public function __construct(\App\Classes\Contract\Role\IRole $_roles) {
    parent::__construct();
    $roles = $_roles;
}

Controller Action method

public function index(IRole $roles) {
    $RoleTypes = $roles->All();
}

So far everything works fine if I keep Interface as parameter in method.

if I try to use the variable $roles in index method and remove the variable, it is always null.

Please guide me if I missed anything?

  • 写回答

1条回答 默认 最新

  • dongmo8943 2016-06-05 12:26
    关注

    You incorrectly assign the $roles property in your __construct() method.

    Replace

    $roles = $_roles;
    

    with

    $this->roles = $_roles;
    

    and then in your index method do:

    $RoleTypes = $this->roles->All();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置