開発メモ

開発用のメモです。

外部アプリ化

TestApp/Application.php
TestApp/Controller/HomeController.php
TestApp/Controller/Provider/HomeControllerProvider.php

/index.php

<?php

require_once dirname( dirname( __FILE__ ) ) . '/vendor/autoload.php';

$app = new TestApp\Application( );
$app->run( );

TestApp/Application.php

<?php

namespace TestApp;

    class Application extends \Silex\Application {

        public function __construct( ) {
            parent::__construct( );
            $this->mount( '', new Controller\Provider\HomeControllerProvider( ) );
        }

    }

TestApp/Controller/HomeController.php

<?php

namespace TestApp\Controller;

    use TestApp\Application;

    class HomeController {
        public function index( Application $app ) {
            return "index";
        }

    }

TestApp/Controller/Provider/HomeControllerProvider.php

<?php

namespace TestApp\Controller\Provider;

    use Silex\Application;
    use Silex\ControllerProviderInterface;

    class HomeControllerProvider implements ControllerProviderInterface {

        public function connect( Application $app ) {

            $controllers = $app[ 'controllers_factory' ];
            $controllers->match( '/', "\\TestApp\\Controller\\HomeController::index" )->bind( 'index' );
            
            return $controllers;
        }

    }
Twitter: @asahina_alice