My own PHP framework, based on the Create your own PHP Framework tutorial of Symfony.
💡
|
|
$ sudo apt install php
$ sudo apt install php-xml
$ sudo apt install php-mbstring
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:
|
Ubuntu (18.04 LTS/Bionic, 20.04 LTS/Focal):
$ sudo apt-get install php-xdebug
-
$ git clone [email protected]:jprivet-dev/php-framework.git
-
$ cd php-framework
-
$ php bin/composer install
-
$ php -S localhost:3000 -t web web/front.php
-
Open your browser on http://localhost:3000/
$ vendor/bin/phpunit tests --color
// 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
}
$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);
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
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
❗
|
The following configuration are provided for PHPStorm 2022.3.2 |
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.
-
Create your own PHP Framework: https://symfony.com/doc/current/create_framework/index.html
-
On recréé Symfony ! (Lior CHAMLA): https://www.youtube.com/playlist?list=PLpUhHhXoxrjdk6VgTUrunQNlVZizBU4WF
Feel free to make comments/suggestions to me in the Git issues section.