php framwork, enforces best practises, easy maintenance, modular.
M=>model
V=>view
C=>controller
An architectural system centered around resources and hypermedia, via HTTP protocols.
get
post
put
patch
delete
a cycle meant for maintaining permanent records in a database setting.
CRUD principles are mapped to REST commands to comply with the goals of RESTful architecture.
- Annotaions
- YML
- XML
- PHP
first update packages:
sudo apt-get update
install mysql, php and apach2
sudo apt-get install mysql-server mysql-client
sudo apt-get install apach2
sudo apt-get install php-cli php-xml libapache2-mod-php
install composer:
sudo apt install curl
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
download symfony cli
wget https://get.symfony.com/cli/installer -O - | bash
with composer:
composer create-project symfony/skeleton myProject
with Symfony cli
symfony new --full SymfonyCourseProject
install local certificate for https
symfony server:ca:install
Launch the demo app
symfony server:start
List all running servers:
symfony server:list
list installed php version
symfony local:php:list
start with a proxy
symfony proxy:start
attach a domain to proxy by default ends with .wip
symfony proxy:domain:attach symfonyproject
show logs:
symfony server:log
start server, stop server and server status
symfony server:start
symfony server:stop
symfony server:status
show all php console command
php bin/console
symfony console
install cors
composer require cors
install bundles --dev for development
composer require yourBundle
remove bundles
composer remove yourBundle
show all recipes
composer recipes
create controller
symfony console make:controller YourController
Doctrine
create an entity
symfony console:make entity
this command allows us to create or update an entity, add fields, property and specify type of each one.
create a migration: allows to create a table corresponding to the entity in our database
before we have to configure the .env file to specify our db configuration
for MySQL:
DATABASE_URL=mysql://db_user:db_password@host:port/db_name?serverVersion=*.*
we can create the configured database if not exists with doctrine:
symfony console doctrine:database:create
we can also drop the configured database:
symfony console doctrine:database:drop
it's recommended to define our conf param in /config/services.yaml.
parameters:
app.myparam
list all parameters:
symfony console debug:container --parameters
list value on a parameter:
symfony console debug:container --parameter=app.myparameter
Access to env var in configuration file:
parameter:
app.param: '%env(MYENVVAR)%'
Resolve in conf file: resole allows to resolve conf params in url or other; for example:
doctrine:
url: '%env(resolve(DATABASE_URL))%'
create a table in our DB with our entities:
By default the name of the table is the entity's one.
we can specify the name of our table in the entity by the annotation @ORM\Table(name="tableName")
symfony console make:migration
show status of migrations:
symfony console doctrine:migrations:status
apply the migration:
symfony console doctrine:migrations:migrate
go back:
symfony console doctrine:migrations:migrate prev
reapply:
symfony console doctrine:migrations:migrate next
create an instance of an entity:
public function myfunction(): Response{
$newEntityInstance = new Entity; //new instance of the entity
$newEntityInstance->setFirstAttribute('someinformations');
$newEntityInstance->setSecondAttribute('somInformations');
$em = $this->getDoctrine()->getManager();//get doctrine EM
$em->persist($newEntityInstance);
$em->flush();
return $this-> render('entity.html.twig');
}
we can install the profiler if we need it only or install debug, which include the profiler.
composer req debug --dev
we can inject dependencies in controllers. list all injectable dependencies:
symfony console debug:autowiring
injection could be done in parameter of our function:
public function myfunction(EntityManager $em):\Symfony\Flex\Response
{
//some code
$em->persist();
$em->flush();
}
first import the repo in your controller to use it
$repo = $em->getRepository(Pin::class);
//or
$repo = $em->getRepository('App\entity\pin');
then you can use the methods of your repo.
$pins = $repo->findAll();
we can simplify the code by injecting the ClassRepository and using compact function for the response.
public function myfunction(ClassNameRepository $repo):Response{
$pins = $repo->findAll();
return $this->render('pins.html.twig', compact('pins'));
}