PHP code example of achrafsoltani / routing-service-provider

1. Go to this page and download the library: Download achrafsoltani/routing-service-provider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

achrafsoltani / routing-service-provider example snippets

 {.php}
$loader = 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();
 {.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');
    }
}