achrafsoltani/routing-service-provider
最新稳定版本:v1.0.4
Composer 安装命令:
composer require achrafsoltani/routing-service-provider
包简介
Routing Service Provider for Silex
README 文档
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');
}
}
统计信息
- 总下载量: 95
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-29