100 days of code - Day 1

hananekacemi

Hanane Kacemi

Posted on April 26, 2021

100 days of code - Day 1

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
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

That's all for today,
Feel free to comment for any suggestions/questions
Have a nice day

💖 💪 🙅 🚩
hananekacemi
Hanane Kacemi

Posted on April 26, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

100 days of code - Day 2
100daysofcode 100 days of code - Day 2

May 2, 2021

100 days of code - Day 1
100daysofcode 100 days of code - Day 1

April 26, 2021