Php
/
Framework
- 1 Basics 5
-
Quotes
-
Constants
-
Control structures
-
Reference
-
Integers
- 2 Variables 4
-
Definition
-
Naming
-
Exists
-
Type casting
- 3 Operators 5
-
Aritmetic
-
Bitwise
-
String
-
Comparison
-
Logical
- 4 Function 4
-
Definition
-
Anonymous
-
Reference
-
Variable scope
- 5 Array 9
-
Basics
-
Operations
-
Create
-
KeyValue
-
Search
-
Modify
-
Sort
-
Stacks
-
Storage
- 6 String 9
-
Basics
-
Compare
-
Search
-
Replace
-
Format
-
Regexp
-
Parse
-
Formating
-
Json
- 7 Streams 6
-
File open
-
Read file
-
Read csv
-
File contents
-
Context
-
Ob_start
- 8 Oop 6
-
Object instantiation
-
Class constructor
-
Interfaces, abstract
-
Resource visibility
-
Class constants
-
Namespaces
- 9 Features 9
-
Autoload
-
Class reflection
-
Magic methods
-
Exceptions
-
Late static binding
-
Type hinting
-
SPL
-
PHPUNIT
-
PHAR
- 10 Versions 2
-
Php7.4
-
Php8.0
- 11 Http 4
-
Headers
-
File Uploads
-
Cookies
-
Sessions
- 12 Design Patterns 4
-
Singleton Pattern
-
Observer Pattern
-
Strategy Pattern
-
Registry
- 13 Modern Php 8
-
Composer
-
Slim Framework
-
Autoloader
-
Package
-
Releases
-
Generators
-
Dependency Injection
-
Middleware
- 14 Create Framework 7
-
App
-
Http Foundation
-
Front Controller
-
Routing
-
Render Controller
-
Resolver
-
SoC
- 15 Frameworks 4
-
Symfony v5
-
Laravel v8
-
Laminas v3
-
Codeigniter v4
- 16 Composer 5
-
Guzzle
-
Carbon
-
Faker
-
Math
-
Requests
- 17 Symfony 6
-
Routes
-
Annotations
-
Flex
-
Controllers
-
Doctrine
-
Templating
/
Render Controller
➟
➟
Last update: 28-03-2022
Front
Lets separate the render template code from the logic.
/**
* Front Controller - public/front.php
*
* We can replace call_user_func argument with any valid callback.
* As a convention, for each route, the associated controller ...
* is $_controller route attribute.
*
* ------------------------------------------------------------
*
* composer require symfony/http-foundation
* composer require symfony/routing
*
* php -S localhost:8000 public/front.php
*
* http://localhost:8000/hello/Fabien
* Hello Fabien
*/
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing;
$request = Request::createFromGlobals();
$routes = include __DIR__.'/../src/app.php';
$context = new Routing\RequestContext();
$context->fromRequest($request);
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);
try {
$request->attributes->add(
$matcher->match($request->getPathInfo())
);
$response = call_user_func( // Render Controller
$request->attributes->get('_controller'), $request
);
} catch(Routing\Exception\ResourceNotFoundException $e) {
$response = new Response('Not found', 404);
} catch (Exception $e) {
$response = new Response('An error occured', 500);
}
$response->send();
Render
This is rather flexible as you can change Response object afterwards.
/**
* App Configuration - src/app.php
*
* This is rather flexible as you can change Response object afterwards.
*/
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
$routes = new RouteCollection();
$routes->add('bye', new Route('bye'));
$routes->add('hello', new Route('/hello/{name}', [
'name' => 'World',
'_controller' => 'render_template' // Render Controller
]));
function render_template($request)
{
extract($request->attributes->all(), EXTR_SKIP);
ob_start();
include sprintf(__DIR__.'/../src/pages/%s.phtml', $_route);
return new Response(ob_get_clean());
}
return $routes;
View
src/pages/hello.phtml
Hello <?= $name ?>
➥ Even App (2/2)Even App
Lets create a new application that say if a number is even or not.
/**
* public/front2.php
*
* php -S localhost:8000 public/front2.php
*
* http://localhost:8000/isEven/24
* Yes, the number is Even
* http://localhost:8000/isEven/123
* No, the number is Odd
*
*/
ini_set('display_errors', 1);
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing;
$request = Request::createFromGlobals();
$routes = include __DIR__.'/../src/app2.php'; // Look Here
$context = new Routing\RequestContext();
$context->fromRequest($request);
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);
try {
$request->attributes->add(
$matcher->match($request->getPathInfo())
);
$response = call_user_func(
$request->attributes->get('_controller'), $request
);
} catch(Routing\Exception\ResourceNotFoundException $e) {
$response = new Response('Not found', 404);
} catch (Exception $e) {
$response = new Response('An error occured', 500);
}
$response->send();
App
We add is even logic before render the template.
/**
* Is Even App - src/app2.php
*/
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing;
$routes = new Routing\RouteCollection();
$routes->add('isEven', new Routing\Route('/isEven/{number}', [
'number' => null,
'_controller' => function($request) {
$message = "The number is Odd";
if (isEven($request->attributes->get('number'))) {
$message = "The number is Even";
}
$request->attributes->set('message', $message);
$response = render_template($request);
return $response;
}
]));
function isEven($number) : bool
{
return $number % 2 == 0;
}
function render_template($request) : Response
{
extract($request->attributes->all(), EXTR_SKIP);
ob_start();
include sprintf(__DIR__.'/../src/pages/%s.phtml', $_route);
return new Response(ob_get_clean());
}
return $routes;
➥ Questions github Framework