100 days of code - Day 1
Hanane Kacemi
Posted on April 26, 2021
Hello,
I decided to participate on "100DaysOfCode" challenge. To start I'm choosing "Create your own PHP Framework" tutorial, available on symfony website : https://symfony.com/doc/current/create_framework/index.html
In the introduction section of this tutorial they asked a question : "why would someone choose to create its own framework instead of using existing ones ?
well, for me, the main raison is : I want to learn more about low level architecture of frameworks.
So, What do we need for this mission :D ?
- PHP 7.4 or later
- web server : nginx, apache, php's built in server (I am unsing xampp)
- OOP knowledge
- Composer to install third-party component
Part 1 : Http Foundation Component :
When we create a web app, we assume that we will have a communication between a client and the server, the client sends a request and the server returns a response.
In PHP, the request is represented by global variables ($_GET, $_POST, $_FILE, $_COOKIE, $_SESSION…) and the response is generated by functions (echo, header, setcookie, …).
HttpFoundation component replaces these default PHP global variables and functions by an object-oriented layer.
we can add this component to our project via :
composer require symfony/http-foundation
There are two main classes : Request and Response
These are some main functions of Request Class :
$request->getPathInfo(); // the URI being requested (e.g. /about) minus any query parameters
// retrieve GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');
// retrieve SERVER variables
$request->server->get('HTTP_HOST');
// retrieves an instance of UploadedFile identified by foo
$request->files->get('foo');
// retrieve a COOKIE value
$request->cookies->get('PHPSESSID');
// retrieve an HTTP request header, with normalized, lowercase keys
$request->headers->get('host');
$request->headers->get('content-type');
$request->getMethod(); // GET, POST, PUT, DELETE, HEAD
$request->getLanguages(); // an array of languages the client accepts
And below are those of Response Class :
$response = new Response();
$response->setContent('Hello world!');
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'text/html');
// configure the HTTP cache headers
$response->setMaxAge(10);
That's all for today,
Feel free to comment for any suggestions/questions
Have a nice day
Posted on April 26, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.