GithubHelp home page GithubHelp logo

php-di-zf1's Introduction

PHP-DI integration with Zend Framework 1

This library provides integration for PHP-DI with Zend Framework 1.

This project also contains the Zend Framework quickstart configured with PHP-DI.

PHP-DI is a Dependency Injection Container for PHP.

Set up

Require the libraries with Composer:

{
    "require": {
        "mnapoli/php-di": "*",
        "mnapoli/php-di-zf1": "*"
    }
}

To use PHP-DI in your ZF1 application, you need to change the Dispatcher used by the Front Controller in the Bootstrap.

    /**
     * Initialize the dependency injection container
     */
    protected function _initDependencyInjection()
	{
		$container = new \DI\Container();

        $dispatcher = new \DI\ZendFramework1\Dispatcher();
        $dispatcher->setContainer($container);

        $frontController = Zend_Controller_Front::getInstance();
        $frontController->setDispatcher($dispatcher);
	}

That's it!

Usage

Now you can inject dependencies in your controllers!

For example, here is the GuestbookController of the quickstart:

use DI\Annotation\Inject;

class GuestbookController extends Zend_Controller_Action
{
    /**
     * This dependency will be injected by PHP-DI
     * @Inject
     * @var Application_Service_GuestbookService
     */
    private $guestbookService;

    public function indexAction()
    {
        $this->view->entries = $this->guestbookService->getAllEntries();
    }

    public function signAction()
    {
        $request = $this->getRequest();
        $form    = new Application_Form_Guestbook();

        if ($this->getRequest()->isPost() && $form->isValid($request->getPost())) {
            $this->guestbookService->addEntry($form->getValues());
            return $this->_helper->redirector('index');
        }

        $this->view->form = $form;
    }

}

Read more about PHP-DI.

Zend Framework quickstart

The quickstart is in the quickstart/ folder. You can look at it to see how PHP-DI can be integrated to a ZF1 application.

To run the quickstart, get composer and install the dependencies:

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

Note: I tried not to diverge too much from the official quickstart. So the code may look messy, but that's how the ZF quickstart is ;)

php-di-zf1's People

Contributors

mnapoli avatar drdamour avatar

Watchers

Paul Price avatar Daniel avatar James Cloos avatar  avatar Jeff Flitton avatar  avatar  avatar  avatar  avatar  avatar Andy Cobb avatar  avatar  avatar balihoo-jmelanson avatar  avatar Dustin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.