achrafsoltani / routing-service-provider
Routing Service Provider for Silex
v1.0.4
2016-01-03 15:16 UTC
Requires
- php: >=5.3.0
- silex/silex: ^1.2
This package is not auto-updated.
Last update: 2024-11-09 18:11:39 UTC
README
Convention-based controllers for Silex
Requirements
- PHP 5.3+
- monolog/monolog (through the MonologServiceProvider)
Installation
$ composer require achrafsoltani/routing-service-provider
Setup
$loader = require_once __DIR__.'/vendor/autoload.php';
// THIS LINE IS MANDATORY, SO THE AUTOLOAD BECOMES AWARE OF YOUR CUSTOM CONTROLLERS
$loader->addPsr4('',__DIR__.'/src/',true);
use Silex\Application;
use AchrafSoltani\Provider\RoutingServiceProvider;
use Symfony\Component\HttpFoundation\Response;
$app = new Application();
$app['debug'] = true;
// Registering
$app->register(new RoutingServiceProvider());
// Defining routes
// You could also implement a custom routes loader from different locations and server a RouteCollection
// instance throough : $app['routing']->addRoutes($routes, $prefix);
$route = new Symfony\Component\Routing\Route('/', array('controller' => 'Foo\Controller\MainController::index'));
// setting methods is optional
$route->setMethods(array('GET', 'POST'));
$route2 = new Symfony\Component\Routing\Route('/hello', array('controller' => 'Foo\Controller\MainController::hello'));
$app['routing']->addRoute('home', $route);
$app['routing']->addRoute('hello', $route2);
// call this rigth before $app->run();
$app['routing']->route();
$app->run();
Example Controller
<?php
namespace Foo\Controller;
use AchrafSoltani\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
// you should extend the base Controller (AchrafSoltani\Controller\Controller) in order to have
// the service container injected
class MainController extends Controller
{
public function index()
{
// services can be accessed as array params: $this->container['key'];
// $this->container is equal to the $app instance
if($this->container['debug'])
{
// or through the get method (symfony Like): $this->container['key'];
return $this->get('twig')->render('form.html.twig');
}
}
public function hello()
{
if($this->get('request')->isMethod('POST'))
{
$username = $this->get('request')->get('username');
return $this->get('twig')->render('hello.html.twig', array('username' => $username));
}
return new Response('no post');
}
}