Sunday, 13 November 2016

Configuring And Installing Symfony


The recommended way to install new Symfony application is through Symfony Installer. This is a symfony application which helps you create new symfony application any number of times.

Enter following commands to your console to create new symfony project in your desired directory (Prerequisites : php must be installed in your system and php.phar extention must be enabled in your php configuration)

For Linux and Mac Users

$ sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
$ sudo chmod a+x /usr/local/bin/symfony

For Windows users

c:\> php -r "readfile('https://symfony.com/installer');" > symfony

After executing above command move the downloaded symfony component  to your projects directory and execute following commands further

c:\> move symfony c:\symfonyProjects 
c:\symfonyProjects\> php symfony

Creating New Symfony Application

Creating/Installing new symfony application is pretty easy. Simply enter following command in your command line tool which will download all the required dependencies to run your project.


#Linux, Mac OS X Users
$ symfony new symfonyninja

#Windows Users
c:\> cd symfonyProjects/
c:\symfonyProjects\> php symfony new symfonyninja

#to create new symfony project with specific version you can simply write
$ symfony new symfonyninja 2.8
$ symfony new symfonyninja 3.1

Running Symfony Application

There are two ways of running symfony application:

1) By using inbuilt symfony console command

$ cd symfonyninja/
$ php bin/console server:run

After that simply type http://localhost:8000/ in your browser to see the Welcome page

2) By writing the absolute path of the project location from your localhost and appending it with /web/app_dev.php. Eg http://localhost/symfonyninja/web/app_dev.php

After following any of the above method you will get welcome screen as this



You must check for symfony configuration and setup before proceeding and rectify then if there are any issues. So type http://localhost:8000/config.php and follow the instructions before moving further.

Saturday, 12 November 2016

Symfony Components



Symfony Components .


So here we are going to talk about the components of symfony , once you understand the different parts and functioning of symfony , things become easy for you .

As in a machine every part is important , in the same way , for symfony framework every part is important , they are as follows :-

So here are the major components which will be used in all the situations hopefully as soon you make a fully fledged application .

they are as follows :-

  • Translation
    a framework which is used for dealing with strings in your application .
  • Security
    a library which will take care of all the security measures need to be taken inside the application with a vast area to cover.
  • Templating
    a nice tool to render templates and beautiful layouts and dealing with all kinds of operations with the twigs or smarty templates and the controllers , what exactly happens is you do the computations and logics inside the controller and send the resulting arrays suppose to the templating engine, and it renders it as the way you want .
  • ClassLoader
    as you would have seen in out examples in symfony ninja or in the snapshots of netbeans on the earlier lines we use 'Use ' keyword , this is to load the class diretly by its name without knowing its whole path and without using the old standards of require and require once .
  • Validator
    its kind of an engine which decides whether the data incoming is proper or not , its validated fine or not , when a user submits a form then it goes to the validator engine to be checked whether its correct or not .
  • Forms
    a fully flexible feature of symfony to deal with forms with full flexibility linking the validation as well.
  • Routing
    as we have already talked about the stuff ,routing and how the front controller works , so this important part routing deal with the pattern matching and deciding giving power to which controller based on the yml files , annotations etc .
  • HTTPFoundation
    i think we have already talked a lot about the http foundation , it has two major classes that are the Request and Response , these two classes are responsible to take up the request and  send an appropriate response .


    Thanks , lets move to the next Chapter now .



Full Story Request TO Response


Full Story Request TO Response (Symfony):-


Exactly like the http , Request and the Response are very simple , the real deal is the code which will be written by you which comes is between which will do the computation and embedding the logics that will take you from the Request to the Response .So we can say that your application can be of many types and obviously you would have made some nice also also , it can produce a web page , a streaming video / audio website , a pdf download , a multipart form data where you can input the form as an upload , these are only some of the examples .So how will you make a lot of epic things in between the request and the response and still keep your source code managed in a well manered way .

Thats why Symfony was made !!!


just to give you a glimpse , here is a picture of the structure , how it keeps everything managed .


Front Controller :-

the applications that you would have already made say would be having 5 parts .

1) hello.php
2) Form.php
3)submission.php
4)computation.php
5)Thankyou.php

so its a simple application where people come and visit you webpage and you ask them to fill a form with their details hence you submit the form and do the validations and computations and if everthing is fine then you send them to the thank you page .

So as the name suggests off all the files , it does the respective work , as the user moves ,pages moves and so on .

The better approach to all this is having a single code or a controller which takes care of all the request coming to it and taking care of sending to different places hence to get the response .








so every application out there made in modern times follow this architecture , all the request will come to it and hence based on the routing , it will decide where to send the details for the logics to work on .


So you can actually get an idea , how the things work here in symfony though we will explain everything in details .


No Worries !!




The Flow :-






When you let symfony handle the requests then you programming becomes easier , and whatever requests comes to you application will follow the same patter which is suggested in the picture above .

the incoming request is taken by the front controller and based on a particular files made for the routing purpose , routing.yml it decides internally which requests comes on which url and hence it sends the particular input data to the respective controller and hence the response is provided sucessfully .


An Example :- 


As you can see at line number 29 to 32 , there is a route defined whenever a request is sent to the /contact route , then front controller takes the job , it checks in the routing files and hence it comes upon the conclusion that Oh! i need to send it to the Maincontroller now for getting the response.


Now we will see the main controller class which will make the idea crystal clear .




Here is the source code also :-


// src/Acme/DemoBundle/Controller/MainController.php
namespace Acme\DemoBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class MainController
{
public function contactAction()
{
return new Response('Symfony Ninja , how are you ?');
}
}


So the front controller decides and send it to the MainController and hence the user gets the response of 

'Symfony Ninja , how are you ?'

Awesome , we are doing great !! Lets move to the next Chapter .





Request and Response In Symfony

Request and Response In Symfony:-

As we have already talked about the request in simple php as this picture will explain it to you and why do we use the headers to make the browsers understand what it has to render and 
show to the end user .








so these two pictures clearly shows how the request response works for simple vanilla php now lets come to the symfony part .




The thing is Symfony provides an alternative way of dealing with the help of two classes which are simple object oriented for the betterment which are Request and Response .







To be specific the source code is here and the explanation is further given :-


use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();

// the URI being requested (e.g. /about) minus any query parameters
$request->getPathInfo();
// retrieve GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');
// retrieve SERVER variables
$request->server->get('HTTP_HOST');
// retrieves an instance of UploadedFile identified by foo
$request->files->get('foo');
// retrieve a COOKIE value
$request->cookies->get('PHPSESSID');
// retrieve an 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();


what is done here is a special class that is request object is taken from the symfony library which contains of all the parameters in the get and post and whatever comes to the particluar api and with the use of this Request object we can take our variables .

In the common scenario how it is done is :-

example , if a json is coming to the function as a request you will fetch all the details like this :-

     $json_data = json_decode($request->getContent(), true);


if you want to get all the post parameters that came inside the request object then you simple fetch it by this code .

$postParams = (array) $request->request->all();

you can get the headers used when sending the requests , get the languages which can be used , the methid used to send the request using code which the most generally used and the best practices used here .

// retrieve an 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(); 


As you can see now that how we accessed the get and post params using " query " method .
These kind of method attributes are called as the parameter objects .

ParameterBags:-



As we used query here , its a parameter object which has methods like,
has()
all()
get()

and so on .



Response in Symfony :-


In the same way symfony provides you with the simple object oriented style of class of the Response . You can include it in the use statements and use it inside your code wherever you want it .

As you can easily make out an understanding from the pictures here ,






or you can simple use it like this ,
return new Response('this is the new response i learned from symfony ninja just now yipee !);

Friday, 11 November 2016

HTTP


Whats Http ? whats the concept ?


http you can say is the language between the two machines , what they understand and based on that how they behave .

its a protocol , a kind of set of rules to understand and respond back among the machines connected on the internet like we connect in english ,latin or french .








Every server side has a http daemon , as soon as you open a web page , it send a request to the server daemon of the respected server side and with proper http headers , now the story starts for the http , it understand the requests and based on that it sends a web page , a file , a song , a video or whatever you want my friend .


this is the whole and soul of http , want to learn more about it , tons of information is present on wikipedia but that wont be mandatory as per now .




HTTP and PHP , request and their response ??

if you have used php and worked out with the requests and response then you would be knowing that php makes you takes the request from some special superglobal arrays .
For Example , this figure will explain it :-




 in these superglobal arrays we capture the request variables and hence use them further .


When we send a response from simple php we send the headers with it to the browser , sending headers means making the respective browser understand whether the stream response is a text ,and image , a binary file , a mp3 song or anything possible which browser can render .

So in this way the request and response is done between php and browser .

Several http codes are there which lets u know what kind of response came , either it a sucess or a failure or some thing else like , request was not appropriate etc .

Just for an idea , i am posting majorly used http codes as per industry standards .







Its a bit different in the case of symfony , lets come to that .












Wednesday, 9 November 2016

PURPOSE




Hello Mates ,

The purpose for making this blog is we dont have any nice documentations for symfony from last many years , so me as a symfony expert want to help all my programmer friends out there , whatever it takes , atleast whatever i know i can share for sure .

its just the starting so maybe all kind of post will not be there so be patient and one thing more , if you go through all the post properly i can assure you , it harly takes 10 days to become a ninja in symfony .

The topics will be covered in chapter wise manner , please start from the beginning then move forward , all the examples are given both in pictures with proper indentations and the source code is also provided .

if you know nothing about symfony then also , you will create great knowledge regarding this super framework in no time .

Feel free to contact me , i am ready to help always as far as my time allows as i have a awesome programming job to do in the morning :)

my facebook id is here :-

https://www.facebook.com/sagarmustaffa

Thanks ,
Amit Sagar Verma.

Symfony ! Why ? What ? How?



















Hello There friends ,

Lets be honest as we are going to learn symfony very fast and as per my plan you will become a master in symfony in maximum 10 days .


There are some pre properties that you need to have:-

1) you must understand the basics of php .
2) you must understand the internet , how the things works like what is url and all .

If you really don't understand these stuff , then also no problem , together we can overcome the issue ,feel free to ping me anytime i will certainly help you .
Now the introductions are done , lets move to the crucial point that why is symfony important to learn and what is it and what's the real purpose behind the superman framework ??

Hold on the horses , all answers will be given  with proper examples in pictures and as well as text pre formatted form also , so that you can easily use it in your notepad , netbeans or phpstorm as you like it my friend .
So i am amusing that you are not that much aware of web .


Why Symfony ?


JUST THE BEGINNING

Symfony is written and compiled by the most awarded php programmers of the world , this framework is strict though but it will take to the basic of web programming and php .


By learning symfony , you are not going to learn just another framework , you are going to learn the best practices used and that will be used in the future for the next 10 years atleast , this will make you more productive , efficient and a big time saver also .The way the routes , requests and response are handled , configuration is set in symfony is remarkable .


Most importantly , there are tons of independent bundle out there which will make programming a piece of cake , you just have to follow the architecture in the best possible way and use their libraries as per need .


So , you can say 60 % of the job is already done by the architecture and folder structure of this framework .Symfony used composer and its concepts , why composer is a big deal we will come to that later in the next chapters .