Skip to content

My own PHP framework, based on the "Create your own PHP Framework" tutorial of Symfony.

License

Notifications You must be signed in to change notification settings

jprivet-dev/php-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Framework

1. Presentation

My own PHP framework, based on the Create your own PHP Framework tutorial of Symfony.

💡
  • Don’t reinvent the wheel!

  • Still have the separation of concerns principle in mind!

2. Prerequisites

2.1. PHP

$ sudo apt install php
$ sudo apt install php-xml
$ sudo apt install php-mbstring

2.2. Composer

Example:

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ mkdir bin
$ php composer-setup.php --install-dir=bin --filename=composer
$ php -r "unlink('composer-setup.php');"

Now run php bin/composer in order to run Composer.

💡

For the Composer autoloader to be updated, run:

$ php bin/composer dump-autoload
Generating autoload files
Generated autoload files

3. Xdebug

Ubuntu (18.04 LTS/Bionic, 20.04 LTS/Focal):

$ sudo apt-get install php-xdebug

4. Installation

  1. $ git clone git@github.com:jprivet-dev/php-framework.git

  2. $ cd php-framework

  3. $ php bin/composer install

  4. $ php -S localhost:3000 -t web web/front.php

  5. Open your browser on http://localhost:3000/

5. Tests

$ vendor/bin/phpunit tests --color

6. Memos

6.1. Request class

// the URI being requested (e.g. /about) minus any query parameters
$request->getPathInfo();

// retrieves GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');

// retrieves SERVER variables
$request->server->get('HTTP_HOST');

// retrieves an instance of UploadedFile identified by foo
$request->files->get('foo');

// retrieves a COOKIE value
$request->cookies->get('PHPSESSID');

// retrieves a 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

// simulate a request
$request = Request::create('/index.php?name=Fabien');
$response->setMaxAge(10);

if ($myIp === $request->getClientIp()) {
    // the client is a known one, so give it some more privilege
}

6.2. 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);

6.3. UrlGenerator class

use Symfony\Component\Routing;

$generator = new Routing\Generator\UrlGenerator($routes, $context);

echo $generator->generate('hello', ['name' => 'Fabien']);
// outputs /hello/Fabien
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

echo $generator->generate(
    'hello',
    ['name' => 'Fabien'],
    UrlGeneratorInterface::ABSOLUTE_URL
);
// outputs something like http://example.com/somewhere/hello/Fabien

6.4. Callables

function my_function(string $name)
{
    var_dump(sprintf('Simple: %s', $name));
}

$callable = 'my_function';

$callable('Fabien');                    // Simple: Fabien
call_user_func($callable, 'Fabien');    // Simple: Fabien
$anonyme = function (string $name) {
    var_dump(sprintf('Closure: %s', $name));
};

$callable = $anonyme;

$callable('Fabien');                    // Closure: Fabien
call_user_func($callable, 'Fabien');    // Closure: Fabien
class MyClass
{
    public function myMethod(string $name)
    {
        var_dump(sprintf('Method: %s', $name));
    }
}

$callable = [new MyClass, 'myMethod'];

$callable('Fabien');                    // Method: Fabien
call_user_func($callable, 'Fabien');    // Method: Fabien
class MyClass
{
    static function myMethod(string $name)
    {
        var_dump(sprintf('Static: %s', $name));
    }
}

$callable = ['MyClass', 'myMethod'];

$callable('Fabien');                    // Static: Fabien
call_user_func($callable, 'Fabien');    // Static: Fabien
class MyClass
{
    public function __invoke(string $name)
    {
        var_dump(sprintf('Invoke: %s', $name));
    }
}

$callable = new MyClass();

$callable('Fabien');                    // Invoke: Fabien
call_user_func($callable, 'Fabien');    // Invoke: Fabien

7. PHPStorm configuration

The following configuration are provided for PHPStorm 2022.3.2

7.1. Enable auto-import in file scope

For importing the classes like:

use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();

Instead of:

$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();

Go on Settings (Ctrl+Alt+S) > Editor > General > Auto Import > PHP and check Enable auto-import in file scope.

9. Comments, suggestions?

Feel free to make comments/suggestions to me in the Git issues section.

10. License

"PHP Framework" is released under the MIT License


About

My own PHP framework, based on the "Create your own PHP Framework" tutorial of Symfony.

Resources

License

Stars

Watchers

Forks