We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

bind interface with class

is there any way to bind interface with class ? like laravel provides the bind function to map interface with class.

App::bind('Raid\Service\Log\LogInterface', 'Raid\Service\Log\Log'); // laravel example

I would like to pass in constructor of class

class UsersController extends AbstractController
{
    private  $users;

    public function __construct(IUsersRepository $usersRepository)
    {
        $this->users = $usersRepository;
    }
}


507

You can do something similar, but based on services:

define controller as a service, which expects session service to be injected:

$di->set(
    'Mynamespace\Frontend\Controller\IndexController', array(
    'className' => '\Mynamespace\Frontend\Controller\IndexController',
    'calls' => array(
        array(
            'method' => 'setMySession',
            'arguments' => array(
                array('type' => 'service', 'name' => 'session'),
            )
        ),
    )
));

Controller code:

namespace Mynamespace\Frontend\Controller;

class IndexController extends ControllerBase
{
    private $mySession;

    public function setMySession(\Phalcon\Session\AdapterInterface $session)
    {
        $this->mySession = $session;
    }

    public function getMySession()
    {
        return $this->mySession;
    }

Thanks Nini, I will try this for sure.