- FEATURES
- Autoload
- Class Reflection
- Magic Methods
- Exceptions
- Late Static Binding
- Type Hinting
- SPL
- PHPUNIT
- PHAR
- COMPOSER
- Carbon
- Guzzle
- Faker
- Math
- Requests
- DESIGN PATTERNS
- Singleton Pattern
- Observer Pattern
- Strategy Pattern
- Dependency Injection
- Middleware
- Registry
- SYMFONY
- Routes
- Annotations
- Flex
- Controllers
- Doctrine
- Templating
- VERSIONS
- Php7.4
- Php8.0
- SECURITY
- Filter Input
- Remote Code Injection
- Sql Injection
- Session Fixation
- File Uploads
- Cross Site Scripting
- Spoofed Forms
- CSRF
- Session Hijacking
- MODERN PHP
- Composer
- Autoloader
- Package
- Releases
- Generators
- Dependency Injection
- Middleware
- CUSTOM FRAMEWORK
- App
- Http Foundation
- Front Controller
-
Routing
- Render Controller
- Resolver
- SoC
- FRAMEWORKS
- Slim
- Symfony V5
- Laravel V8
- Laminas V3
- Codeigniter V4
Routing
One important aspect of any website is the form of the URLs.
/**
* Front Controller - public/front.php
*
* Following the MVC pattern is not the right goal.
* The main goal should be Separartions of Concerns SoC.
*
* Route configuration moved to its own file (app.php)
* Routes names are used for template names.
*
* Based on $routes information (RouteCollection instance),
* a UrlMatcher instance can match URL paths.
*
* The matched route is automaticaly stored in _route attribute.
* Attributes are extracted to keep template simple.
*
* The request context is not strictly needed,
* but it is used in real-world applications
* to enforce method requirements and more.
*
* ----------------------------------------------------------
*
* composer require symfony/http-foundation
* composer require symfony/routing
*
* php -S localhost:8000 public/front.php
*
* http://localhost:8000/hello/Fabien
* Hello Fabien
* http://localhost:8000/bye
* Goodbye!
*/
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
$request = Request::createFromGlobals();
$routes = include __DIR__.'/../src/app.php';
$context = new RequestContext();
$context->fromRequest($request);
$matcher = new UrlMatcher($routes, $context);
try {
extract($matcher->match($request->getPathInfo(), EXTR_SKIP));
// $_route = hello | bye
// $name = Fabien | World
ob_start();
include sprintf(__DIR__.'/../src/pages/%s.phtml', $_route);
$response = new Response(ob_get_clean());
} catch(ResourceNotFoundException $e) {
$response = new Response('Not found', 404);
} catch (Exception $e) {
$response = new Response('An error occured', 500);
}
$response->send();
App
We separated the configuration specific to our application in app.php
/**
* App Configuration - src/app.php
*/
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
$routes = new RouteCollection();
$routes->add('hello',
new Route('/hello/{name}', ['name' => 'World'])
);
$routes->add('bye',
new Route('/bye')
);
return $routes;
Last update: 531 days ago