September / October 2021
🔨 From udemy: Symfony : projet complet par la pratique - Florent NICOLAS.
Symfony application to make monitoring of websites and display their http status. Admin can add, delete, update the websites in database (MySQL).
Demo online: Symfony: monitoring
- Clone
Composer update
- Start Apache:
sudo service apache2 start
- Start MySQL:
sudo service mysql start
- In MySQL:
mysql> CREATE DATABASE monitoring
- Seed:
php bin/console doctrine:fixtures:load
You will need to provide credentials (for the app) and datas in src/DataFixtures - Provide your MySQL datas/credentials in .env.local.php EX:
<?php
// This file was generated by running "composer dump-env prod"
return array (
'APP_ENV' => 'dev',
'APP_SECRET' => '125222929016dc969867c704e513020e',
'DATABASE_URL' => 'mysql://<USER-NAME>:<PASSWORD>@127.0.0.1:3306/monitoring',
);
symfony server:start
symfony open:local
or http://localhost:8000/
Doctrine ORM is an object-relational mapper (ORM) for PHP 7.1+ that provides transparent persistence for PHP objects. It uses the Data Mapper pattern at the heart, aiming for a complete separation of your domain/business logic from the persistence in a relational database management system.
The benefit of Doctrine for the programmer is the ability to focus on the object-oriented business logic and worry about persistence only as a secondary problem. This doesn't mean persistence is downplayed by Doctrine 2, however it is our belief that there are considerable benefits for object-oriented programming if persistence and entities are kept separated.
Fixtures are used to load a "fake" set of data into a database that can then be used for testing or to help give you some interesting data while you're developing your application.
Entities are PHP Objects that can be identified over many requests by a unique identifier or primary key. These classes don't need to extend any abstract base class or interface. An entity class must not be final or contain final methods. Additionally it must not implement clone nor wakeup, unless it does so safely.
An entity contains persistable properties. A persistable property is an instance variable of the entity that is saved into and retrieved from the database by Doctrine's data mapping capabilities.
In this app, we have three entities: Website, Admin and Status.
The double arrow operator => (assign)
The arrays are accessed with the use of a double arrow operator. In other terms, the operator is also used to assign a certain value to an acceptable type of operator in the array index which can be in the form of either numeric or string-based (associative). Moreover, double arrow operator => assigns the value to an array key.
The object operator -> (access)
The -> operator, also known as the object operator is used to access the properties and methods for a specific object. Besides, in simple words, the object operator -> is responsible for accessing an object method.
Scope resolution operator ::
:: is called as scope resolution operator (AKA Paamayim Nekudotayim). This operator is used to refer the scope of some block or program context like classes, objects, namespace and etc. For this reference an identifier is used with this operator to access or reproduce the code inside that scope.
cURL stands for the client URL. PHP cURL is a library that is the most powerful extension of PHP. It allows the user to create the HTTP requests in PHP. cURL library is used to communicate with other servers with the help of a wide range of protocols.
cURL allows the user to send and receive the data through the URL syntax. cURL makes it easy to communicate between different websites and domains.
DataFixtures/AppFixtures.php
// ..
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class AppFixtures extends Fixture
{
public function __construct(UserPasswordHasherInterface $passwordHasher)
{
$this->encoder = $passwordHasher;
}
public function load(ObjectManager $manager)
{
$admin = new Admin();
$admin->setEmail('xxx.xxx.com')
->setPassword('xxx');
$encoded= $this->encoder->hashPassword($admin, $admin->getPassword());
$admin->setPassword($encoded);
$manager->persist($admin );
//..
}
// ..
}
Entity/Admin.php
// ..
public function setEmail(string $email): self {
$this->email = $email;
return $this;
}
public function getPassword(): ?string {
return $this->password;
}
public function setPassword(string $password): self {
$this->password = $password;
return $this;
}
//..
config/packages/security.yaml
password_hashers:
App\Entity\Admin: "auto"
php bin/console doctrine:fixtures:load
config/packages/security.yaml
access_control:
- { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
Or
_Controller/AdminController.php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
// ...
/**
* @Route("/admin", name="admin_dashboard")
* @IsGranted("ROLE_ADMIN")
*/
public function index(WebsiteRepository $repository)
{
// ...
}
// ...
cd /
cd /var/www/html
symfony new --full my_project
Give the rights to a directory: sudo chmod -R 777 /var/www/html/my_project
php bin/console about
sudo apt remove composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
sudo composer -v
symfony server:start
symfony open:local
or http://localhost:8000/
sudo service apache2 start
sudo service apache2 restart
sudo service apache2 stop
sudo service apache2 status
sudo service apache2 -v
sudo service mysql start
sudo service mysql restart
sudo service mysql stop
sudo service mysql status
sudo service mysql -v
sudo mysql
CREATE USER 'USER_NAME'@'%' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO 'USER_NAME'@'%' WITH GRANT OPTION;
use mysql;
SELECT User FROM user;
CREATE DATABASE DB_NAME;
Show DATABASES;
Or to check if the connexion is done, in terminal:
mysql -u 'USER_NAME -p
- Right click on any class name and select Import Class to automatically import it's namespace at the top of the file.
- Press CTRL + ALT + i on Windows / CTRL + Option + i on MacOS.
php bin/console make:controller <ControllerName>
php bin/console make:entity
<EntityName>
-- <PropertyName>
---- <FieldName>
Migration:
php bin/console make:migration
If problem try: sudo apt-get install php-mysql
php bin/console doctrine:migration:migrate
php bin/console make:entity <ExistingEntityName>
-- <PropertyName>
---- <FieldName>
php bin/console make:migration
php bin/console doctrine:migration:migrate
- symfony/orm-pack: A pack for the Doctrine ORM.
sudo composer require symfony/orm-pack
- symfony/maker-bundle: Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
sudo composer require symfony/maker-bundle
- twig/twig: Twig, the flexible, fast, and secure template language for PHP.
sudo composer require twig/twig
- DoctrineFixturesBundle: Fixtures are used to load a "fake" set of data into a database that can then be used for testing or to help give you some interesting data while you're developing your application.
sudo composer require --dev doctrine/doctrine-fixtures-bundle
It creates ./src/DataFixtures.
Use php bin/console doctrine:fixtures:load
to flush.
config/packages/twig.yaml
# config/packages/twig.yaml
twig:
form_themes: ["bootstrap_5_layout.html.twig"]
- WSL2-Windows Linux Subsystem: A guide to install a Local Web Server Ubuntu-20.04 Apache,PHP8 y MySQL8
- Symfony CLI
- How to Use PHP’s built-in Web Server
- Getting Started with Doctrine
- DDD et MVC: différence entre 'Model' et 'Entity'
- Symfony Encore
- Symfony – Comment mettre en place des Fixtures
- Fonctions cURL
- L'opérateur de résolution de portée (::)
- PasswordHasher Component
- Flash Messages
- Validation Constraints Reference
- Tutoriel Symfony : Héberger le site sur un hébergement mutualisé