Create your First Page in Symfony – Route and Controller

We need a code editor, for PHP you can use NetBeans (https://netbeans.org/features/php/) or PHP Storm (https://www.jetbrains.com/phpstorm/)

Inside the project folder there are some directory:
– app
– bin
– src
– tests
– var
– vendor
– web
– composer.json
– LICENSE etc…

Focus on:

A. src: it holds all PHP files (classes)

B. app: it holds configuration, templates

Let’s move on!

1. Go to src/AppBundle/Controller/DefaultController.php, this in the default welcome page, delete it.
Now we have a real empty project :)

2. We need to create a ‘route’ and a ‘controller’
The route is where is the page, its url in poor words.
The controller is a software program that manages or directs the flow of data between two entities, it is the function that build the page.

3. Create inside AppBundle\Controller\GenusController.php

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;  // namespace for route
use Symfony\Component\HttpFoundation\Response;               // namespace for http render

class GenusController 
{
	/**
	* @Route("/genus")
	*/
	public function showAction() // this is the controller that create the page, returns a Response Object
	{
		return new Response('Under the <strong>Sea!</strong>'); // HTTP Text Render
	}
	
}


NOTICE:

– namespace AppBundle\Controller … use etc…: a Symfony namespace necessary for development

– class GenusController: the name of php file GenusController.php
We have created a class named GenusController that can be find by Symfony at src\AppBundle\Controller

– We use this particular syntax to define the http route of the page

/**
* @Route(“/genus”)
*/

It will be http://localhost/symfonytest/first_test_symfony/web/genus

– Controller to create the page:


	public function showAction() // this is the controller that create the page, returns a Response Object
	{
		return new Response('Under the <strong>Sea!</strong>'); // HTTP Text Render
	}
	

Run from the browser: http://localhost/symfonytest/first_test_symfony/web/genus

Official Link:
Ref: http://knpuniversity.com/screencast/symfony/first-page

By |PHP, Symfony, Web Design|Commenti disabilitati su Create your First Page in Symfony – Route and Controller

Symfony for Beginners – Home Pages and .twig Templates – Dev Setup

Create your First Home Page in Symfony

Environment Setup – Automatic Clear Cache

At the end of installation process will have: C:\wamp64\www\symfonytest>cd first_test_symfony

Inside the folder will be the folder structure of my project:
– app
– bin
– src
– tests
– var
– vendor
– web
– composer.json
– LICENSE etc…

Open your broser at: http://localhost/symfonytest/first_test_symfony/web/ to see the welcome page

Go to web\app.php and change the line:

$kernel = new AppKernel(‘prod’, false);

to

$kernel = new AppKernel(‘prod’, true);

This will activate – (‘prod’, true) – the production environment, this mean cache will not be saved, you can change file and reload in browser on the fly and see changes.

If – (‘prod’, false) – you can not reload from browser code changes

Go to var\cache\dev and var\cache\prod to see cache files for ‘dev’ and ‘prod’ environment.

Net Beans IDE

1. Run Net Beans IDE

2. File> New Project> PHP> PHp Application with Existing Sources> NEXT> Browse: C:\wamp64\www\symfonytest>first_test_symfony

HTML Render

1. Controller Request

Go to src\AppBundle\Controller\ -> here you will find all php codes for controllers
Open src\AppBundle\Controller\DeafaultController.php
Change the render page to indexmy.html.twig


return $this->render('default/indexmy.html.twig ...

2. .twig.html

Go to app\Resources\views\default\
and create indexmy.html.twig


{% extends 'base.html.twig' %}

{% block body %}
<h1>This is my first home page</h1>
{% endblock %}

{% block stylesheets %}
<style>
    h1 { font-size: 36px; }
</style>
{% endblock %}

Notice that {% extends ‘base.html.twig’ %}

Go to app\Resources\views\base.html.twig


<!DOCTYPE html>

<html>
    
<head>
<meta charset="UTF-8" />
        
<title> {% block title %}Welcome{% endblock %}</title>
        
	{% block stylesheets %}{% endblock %}
        
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    
</head>
    
<body>
        
	{% block body %}{% endblock %}
        
	{% block javascripts %}{% endblock %}
    
</body>

</html>

The process is:

return $this->render(‘default/indexmy.html.twig: -> call html.twig template -> extend base.html.twig

The rendered code inside the browser will be:


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta charset="UTF-8">
    <title>Welcome!</title>
	<style>
		h1 { font-size: 36px; }
	</style>
    <link rel="icon" type="image/x-icon" href="http://localhost/symfonytest/first_test_symfony/web/favicon.ico">
</head>
<body>
    <h1>This is my first home page</h1>
</body>
</html>

We can write:


{% extends 'base.html.twig' %}

{% block body %}
<h1>This is my first home page</h1>
<strong>Test</strong> HTML tags
<br>
<a href="http://www.lucedigitale.com">Visit Luce Digitale</a>
<br>
{% include 'my_code.html.twig' %}

{% endblock %}

{% block stylesheets %}
<style>
    h1 { font-size: 36px; }
</style>
{% endblock %}

Notice:
{% include ‘my_code.html.twig’ %} -> include a HTML code from a file in app\Resources\views\my_code.html.twig

Warning: you can not write native php code inside a .twig template, you need create an Extension Class (official docs at: http://symfony.com/doc/current/templating/twig_extension.html)

By |PHP, Symfony, Web Design|Commenti disabilitati su Symfony for Beginners – Home Pages and .twig Templates – Dev Setup

Symfony for Beginners – Installation on Windows Machine

What is Symfony? (https://symfony.com)
Symfony is a PHP web application framework and a set of reusable PHP components/libraries. Symfony was published as free software on October 18, 2005 and released under the MIT license.

Alternatives to Symfony are:
– Django (Python)
– Ruby on Rails (Ruby)

Let’s start!!!

WAMP INSTALLATION

1. First we need to install Apache, PHP and MySQL on our Windows Machine.
we can install them separately or installa a PHP development environment, that installs a ready to use environment. There are a lot of software for a dev PHP environment, the most popular are: WAMP XAMPP AMPPS. I will use WAMP.

a. Download WAMP at http://www.wampserver.com/en/#download-wrapper and launch the installer

b. Choose your .exe browser, I will use “C:\Program Files (x86)\Google\Chrome\Application\chrome.exe” because I love the tools for developers of this browser.

Choose yout txt editor, for me is: “C:\Program Files (x86)\Notepad++\notepad++.exe”

c. Open theWindows firewall ports

d. Config the mail() function of PHP

… or leave the default parameters

If installation succeed you will find WAMP options in the notification bar nera your clock.

The WAMP icon will be green: all services running

LMB on WAMP icon:
– Local Host
– phpMyAdmin etc…

RMB on WAMP icon:
– Wamp Settings
– tools
– exit etc…

To avoid conflicts in port 80 with Skype, open Skype> Strumenti> Opzioni> Avanzate> Connessione> uncheck ‘Usa le porte 80 e 443 per le connessioni in ingresso aggiuntive’

WAMP TEST SCRIPT

Now I will test WAMP.

– Click LMB WAMP icon> www directory
– Create here a ‘test’ folder and a test script ‘index.php’


<?php

$name = "Andrew";
echo "My name is " . $name;

?>

– open the browser and digit http://localhost/test/

– You can also LMB WAMP icon> localhost, you will find under ‘Your project’ the ‘test’ folder, but you can not run with a simple click, use the browser at http://localhost/test/ to do it!!!

WAMP phpMyAdmin

– Click LMB WAMP icon> localhost under ‘Tools’ tab phpmyadmin> user ‘root’, pass leave blank

or call with browser http://localhost/phpmyadmin/

SYMFONY INSTALLATION on WAMP with COMPOSER

1. https://getcomposer.org/

2. Download> https://getcomposer.org/Composer-Setup.exe

3. Run as Administrator

4. wamp64/bin/php/php7.0.10/ php.exe

5. Win button> cmd.exe> C:\Users\HAF922\composer and composer will run

START A PROJECT

1. Go to the project folder, example symfonytest> SHIFT+RMB> ‘Apri finestra di comando qui

2. Win button> cmd.exe> composer create-project symfony/framework-standard-edition first_test_symfony
You can use Copy/Paste Win standard function if you want.

3. Download and installation start.
database_host 127.0.0.1: ok is local host
database_port: 3306
database_name: symfony
database_user: root
database_password: 123

secret: 123456

At the end the prompt will have: C:\wamp64\www\symfonytest>cd first_test_symfony

Inside the folder will be the folder structure of my project:
– app
– bin
– src
– tests
– var
– vendor
– web
– composer.json
– LICENSE etc…

Open your broser at: http://localhost/symfonytest/first_test_symfony/web/

Grats!!!

NOTES for Bluehost users at point 3. uses settings similar to the following:

database_driver: pdo_mysql
database_host: localhost
database_port: ~
database_name: (cpanelUsername_databaseName)
database_user: (cpanelUsername_databaseUsername)
database_password: (whatever you picked)
Example:

database_driver: pdo_mysql
database_host: localhost
database_port: ~
database_user: joe1337
database_name: joe1337_sym1
database_password: eHTb7%Pxa9

By |PHP, Symfony, Web Design|Commenti disabilitati su Symfony for Beginners – Installation on Windows Machine