Giter Site home page Giter Site logo

symfonyintro's Introduction

Symfony

php framwork, enforces best practises, easy maintenance, modular.

MVC architecture

M=>model
V=>view
C=>controller

REST (Representational state transfer)

An architectural system centered around resources and hypermedia, via HTTP protocols.

get
post
put
patch
delete

CRUD (create, read, update, 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.

configuration file format in symfony:

  1. Annotaions
  2. YML
  3. XML
  4. PHP

environment:

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

Create a symfony application:

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

ORM (object relational mapper)

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

configuration parameter

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))%'

Migrations

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');
}

profiler and debug

we can install the profiler if we need it only or install debug, which include the profiler.

composer req debug --dev

dependance injections

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();
}

Repository

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'));
}

symfonyintro's People

Contributors

arezki-k avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.