- 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
Single Entry Application
A single entry PHP script is called front controller design pattern. Let's add another page that says goodbye.
/*
front_controller/public/front.php - Single Entry Application
Request::getPathInfo() returns the front controller script name.
$map associates URL paths with their corresponding PHP script paths.
cd github/php-pages/main/framework/front_controller/
composer require symfony/http-foundation
php -S localhost:8000 public/front.php
http://localhost:8000/hello # Hello World
http://localhost:8000/bye # Goodbye!
http://localhost:8000/front.php # Page not found
*/
ini_set('display_errors', 1);
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$request = Request::createFromGlobals();
$response = new Response();
$map = [
'/hello' => __DIR__ . '/../src/pages/hello.phtml',
'/goodby' => __DIR__ . '/../src/pages/goodby.phtml',
];
$path = $request->getPathInfo();
if (isset($map[$path])) {
ob_start();
require($map[$path]);
$response->setContent(ob_get_clean()); // Look here
} else {
$response->setStatusCode(404);
$response->setContent("Page not found");
}
$response->send();
View Template
The hello.phtml script can now be converted to a template.
Hello <?= htmlspecialchars(
$request->get('name', 'World'), ENT_QUOTES, 'UTF-8');
?>
Vanilla PHP
Here's how the provided example could be rewritten using plain PHP.
ini_set('display_errors', 1);
$map = [
'/hello' => __DIR__ . '/../src/pages/hello.phtml',
'/goodby' => __DIR__ . '/../src/pages/goodby.phtml',
];
// Manually fetching the requested path
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (isset($map[$path])) {
ob_start();
include($map[$path]);
$content = ob_get_clean(); // Capture the output of the included file
// Setting the response content
echo $content;
} else {
// Handling a 404 not found
header("HTTP/1.0 404 Not Found");
echo "Page not found - Vanilla PHP";
}
Apache Httpd
With httpd server we can use htaccess to always access front.php.
RewriteEngine on
RewriteBase /php/framework/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ front.php [L,QSA]
Last update: 395 days ago