• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Model + Services

#1
Documentation says


Quote:It is recommended to only create services within controllers. Other files, like models and libraries should have the dependencies either passed into the constructor or through a setter method.


In Services.php i have 6 such methods as 

PHP Code:
public static function permissions($getShared true)
    {
        if ($getShared)
        {
            return static::getSharedInstance('permissions');
        }

        return new \App\Libraries\Permissions();
    

I have to write in every model

PHP Code:
class SomeModel extends Model {

    protected $permissions;
    protected $limiters;
    protected $vertical;
    protected $abs_field;
    protected $forater;
    protected $statistic;
        
    
public function __construct(
        \App\Libraries\Permissions &$permissions
        \App\Libraries\Limiters &$limiters
        \App\Libraries\Vertical &$vertical
        \App\Libraries\Abs_field &$abs_field
        \App\Libraries\Forater &$forater
        \App\Libraries\Statistic &$statistic )
    {
        $this->$permissions =& $permissions;
        $this->$limiters =& $limiters;
        $this->$vertical =& $vertical;
        $this->$abs_field =& $abs_field;
        $this->$forater =& $forater;
        $this->$statistic =& $statistic;
    
The services provided are global. Can be run in the controller and model. Is there a better practice? In fact, it needs global methods. Something like a helper but from App \ Libraries
Reply

#2
In your Model's constructor, no need to pass the services as arguments. You can just call

$this->permissions = Services::permissions();

Do the same with the others you need.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.