programming

Symfony 1.4.20 – Redirect – Forward

Using Plain PHP

1. apps/frontend/modules/contenuto/actions/actions.class.php


<?php

class contenutoActions extends sfActions // estende la classe Symfony
{
  public function executeRedirect() // crea l'indirizzo - contenuto/redirect
  { 
  }// END function
}// END class

2. apps/frontend/modules/contenuto/templates/redirectSuccess.php


<p>Redirect to...</p>

<?php
header("location: http://www.lucedigitale.com");
exit;
?>

3. Point the browser at: http://localhost/jobeet/web/contenuto/redirect

redirect() function External Link

1. apps/frontend/modules/contenuto/actions/actions.class.php


<?php

class contenutoActions extends sfActions // estende la classe Symfony
{
  public function executeRedirect() // crea l'indirizzo - contenuto/redirect
  { 
      return $this->redirect('http://www.google.com');
  }// END function
}// END class

2. apps/frontend/modules/contenuto/templates/redirectSuccess.php


<p>Redirect to...</p>

3. Point the browser at: http://localhost/jobeet/web/contenuto/redirect

redirect() function Internal Link

1. apps/frontend/modules/contenuto/actions/actions.class.php


<?php

class contenutoActions extends sfActions // estende la classe Symfony
{
  public function executeRedirect() // crea l'indirizzo - contenuto/redirect
  { 
      $this->redirect('contenuto/mypage');// nomemodulo/nomepagina
      // oppure sintassi alternativa
      // $this->forward('nomemodulo', 'nomepagina');

      // il codice qui sotto non verrà eseguito perchè
      // redirect e forward sollevano un sfStopException per bloccare l'esecuzione di un'azione

  }// END function
  public function executeMypage() // crea l'indirizzo - contenuto/mypage
  { 
  }// END function
}// END class

NB: redirect cambia URL nel browser, forward NON cambia URL nel browser

2. apps/frontend/modules/contenuto/templates/redirectSuccess.php


<p>Redirect to...</p>

3. Point the browser at: http://localhost/jobeet/web/contenuto/redirect
It will render http://localhost/jobeet/web/contenuto/mypage

Redirect e Forward Condizionali

Symfony ha dei metodi condizionali per snellire la scrittura del codice, in particolare:
forwardIf(), forwardUnless(), forward404If(), forward404Unless(), redirectIf() e redirectUnless().


public function executeShow(sfWebRequest $request)
{
  // Doctrine cerca un parametro id nel DB
  $article = Doctrine::getTable('Article')->find($request->getParameter('id'));
 
  // Propel cerca un parametro id nel DB
  $article = ArticlePeer::retrieveByPK($request->getParameter('id'));
 
  // se non esiste dai errore 404 di Symfony
  if (!$article)
  {
    $this->forward404();
  }

Sintassi equivalente:


public function executeShow(sfWebRequest $request)
{
  // cerca nel DB
  $article = Doctrine::getTable('Article')->find($request->getParameter('id'));
  $this->forward404If(!$article); // errore 404 se non esiste
}
 

public function executeShow(sfWebRequest $request)
{
  // Cerca nel DB
  $article = Doctrine::getTable('Article')->find($request->getParameter('id'));
  $this->forward404Unless($article); // errore 404 a meno che esista, se esiste non da errore
}

Reference:
http://www.symfony-project.org/api/1_4/sfaction

By |PHP, Symfony, Web Design|Commenti disabilitati su Symfony 1.4.20 – Redirect – Forward

Symfony 1.4 – Multi Page – Pass data to Template and Render

Creare la cartella di progetto

1. Create the folder c:\wamp64\www\jobeet

Creare la struttura di cartelle

2. Inside \jobeet folder run the command:
cmd.exe -> c:\wamp64\www\jobeet\symfony generate:project jobeet –orm=Propel

Tris create the basic folders

Creare la app frontend

3. Copy C:\wamp64\bin\php\symfony.bat to c:\wamp64\www\jobeet\

4. cmd.exe -> c:\wamp64\www\jobeet\symfony generate:app frontend

This create the base content of \apps

Creare il contenuto

5. cmd.exe -> c:\wamp64\www\jobeet\symfony generate:module frontend contenuto

Crea il contenuto per \modules

6. Per ogni nuovo modulo, Symfony crea una azione index predefinita, puntiamo il browser a:

http://localhost/jobeet/web/contenuto/index

7. Apriamo il file apps/frontend/modules/contenuto/actions/actions.class.php e scriviamo


<?php

class contenutoActions extends sfActions // estende la classe Symfony
{
  public function executeAboutme() // crea l'indirizzo - contenuto/aboutme
  { 
  }// END function
  
  public function executeContact() // crea l'indirizzo - contenuto/contact
  { 
  }// END function
}// END class

8. Creiamo apps/frontend/modules/contenuto/templates/aboutmeSuccess.php

<p>About me: I am <strong>Andrea</strong></p>

9. Creiamo apps/frontend/modules/contenuto/templates/contactSuccess.php

<p>Contact: My telephone number is 0123456789</p>

10. Puntiamo il browser a:
– http://localhost/jobeet/web/contenuto/aboutme
– http://localhost/jobeet/web/contenuto/contact

Come funziona?

I template dovranno essere nominati seguendo la regola:
aboutmeSuccess.php -> nomelink + Success

I metodi seguiranno la regola:
executeContact() -> execute + (Maiuscola)nomelink

Passare i dati al Template

1. action.class.php


<?php

class contenutoActions extends sfActions // estende la classe Symfony
{
  public function executeRandom() // crea l'indirizzo - contenuto/random
  { 
      $number = mt_rand(0, 100); // this is plain PHP
      // invia nella variabile del template numerocasuale in valore $number
      $this->numerocasuale = $number;
  }// END function
  
}// END class

2. randomSuccess.php


<p>The random number is <strong><?php echo $numerocasuale?></strong></p>

3. Puntare il browser a: http://localhost/jobeet/web/contenuto/random

Abbiamo due tipo di sintassi per assegnare le variabili da passare ai template:


    // Impostare le variabili dell'azione per passare informazioni al template
    $this->pippo = 'pluto';          // Versione breve
    $this->setVar('pippo', 'pluto'); // Versione estesa

Reference:
http://symfony.com/legacy/doc/gentle-introduction/1_4/it/04-The-Basics-of-Page-Creation

By |PHP, Symfony, Web Design|Commenti disabilitati su Symfony 1.4 – Multi Page – Pass data to Template and Render

Install Symfon y1.4.20 on WAMP and WIN7 with PEAR

PEAR installation

PEAR is a framework and distribution system for reusable PHP components.

0. Start WAMP

1. Download PEAR at http://pear.php.net/go-pear.phar

The phar extension provides a way to put entire PHP applications into a single file called a “phar” (PHP Archive) for easy distribution and installation.

2. Copy go-pear.phar into C:\wamp64\bin\php\php5.6.25

3. On Win Button type cmd.exe, RMB ‘Run as Administrator’ and navigate to C:\wamp64\bin\php\php5.6.25

4. Input in the command window: php go-pear.phar
– Are you installing a system-wide PEAR or a local copy?: leave default (system:loca) [system]
– Below is suggested file layout… (locazioni proposte per l’installazione): ENTER to go on
– Would you like to later php.ini?: Y (includerà il path per il pear)
– Enter to continue: ENTER

5. C:\wamp64\bin\php\php5.6.25 double click Pear_ENV.reg (aggiunta al registro di sistema)

PEAR is installed

Nota: per vedere il registo di sistema cliccare sul bottone di windows, in cerca programmi e file scrivere ‘regedit’

Environment variables

1. Bottone di Windows> RMB Computer> Proprietà> Impostazioni di sistema avanzate> Avanzate> Variabili d’ambiente> Variabili di sistema> Variabile Path> Modifica…>aggiungere il separatore ; e C:\wamp64\bin\php\php5.6.25

Symfony 1.4

1. On Win Button type cmd.exe, RMB ‘Run as Administrator’ and navigate to C:\wamp64\bin\php\php5.6.25

2. Type the command: pear channel-discover pear.symfony-project.com (aggiunge a PEAR il canale di Symfony)

3. Type the command: pear install symfony/symfony (scarica symfony-1.4.20.tgz)

At the end you will have the message install ok: …

4. Type: C:\wamp64\bin\php\php5.6.25\symfony -V (message: symfony version 1.4.20), l’installazione è verificata
NB: il comando symfony -V è visto da tutte le cartelle perchè il percorso è registrato

Project Creation

0. We want to create a project named ‘jobeet’

1. Create the folder c:\wamp64\www\jobeet

NB: Windows users are advised to run symfony and to setup their new project in a path which contains no spaces

2. Inside \jobeet folder run the command:
cmd.exe -> c:\wamp64\www\jobeet\symfony generate:project jobeet –orm=Propel

Symfony will create the folder structure:

apps/ Hosts all project applications -> al momento è vuota
cache/ The files cached by the framework
config/ The project configuration files
lib/ The project libraries and classes
log/ The framework log files
plugins/ The installed plugins
test/ The unit and functional test files
web/ The web root directory

NB: The generate:project task has also created a symfony shortcut in the project root directory to shorten the number of characters you have to write when running a task.

App Creation

1. Copy C:\wamp64\bin\php\symfony.bat to c:\wamp64\www\jobeet\

2. cmd.exe -> c:\wamp64\www\jobeet\symfony generate:app frontend

Symfony will create the folder structure:

apps/config
apps/i18n
apps/lib -> libraries
apps/modules -> code MVC
apps/templates

3. Set the write permission for jobeet\cache – jobeet\log – chmod 777, permit read,write,execute

RMB sulla cartella cache> Proprietà> Condivisione> Condivisione avanzata> Condividi la cartella> Autorizzazioni> Consenti> attivare Controllo completo, Modifia, Lettura

RMB sulla cartella log> Proprietà> Condivisione> Condivisione avanzata> Condividi la cartella> Autorizzazioni> Consenti> attivare Controllo completo, Modifia, Lettura

4. Set Apache
LMB over WAMP Icon> Apache> httpd.conf

Copy and paste in the end:

# -------------------------------------------------------------------------------
# Project jobeet START

# Be sure to only have this line once in your configuration
NameVirtualHost 127.0.0.1:8080

# This is the configuration for your project
Listen 127.0.0.1:8080

# 127.0.0.1:8080 da browser punta a c:\wamp64\www\jobeet\web
<VirtualHost 127.0.0.1:8080>
  DocumentRoot "c:\wamp64\www\jobeet\web"
  DirectoryIndex index.php
  <Directory "c:\wamp64\www\jobeet\web">
    AllowOverride All
    Allow from All
  </Directory>

  Alias /sf "c:\wamp64\www\jobeet\lib\vendor\symfony\data\web\sf"
  <Directory "c:\wamp64\www\jobeet\lib\vendor\symfony\data\web\sf">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

# Project jobeet END
# -------------------------------------------------------------------------------

NB: The /sf alias gives you access to images and javascript files needed to properly display default symfony pages and the web debug toolbar|Web Debug Toolbar.

LMB over WAMP Icon> Restart All services

5. Open the browser and point it at: 127.0.0.1:8080 or at http://localhost/jobeet/web/index.php

If you do not see images you have to correct the code addet in httpd.conf.

Reference:
http://symfony.blog.gogo.mn/read/entry350425
https://blog.rolandl.fr/59-installer-symfony-1-4-avec-pear
http://symfony.com/legacy
http://symfony.com/legacy/doc

By |PHP, Symfony, Web Design|Commenti disabilitati su Install Symfon y1.4.20 on WAMP and WIN7 with PEAR

Unity Programming – Classes Inheritance (Ereditarietà) – Intermediate – CSharp

How does classes Inheritance work in Unity?

Create an Empty Object and attach the scripts.

FruitSalad.cs


public class FruitSalad : MonoBehaviour // estende MonoBehaviour, è la classe dalla quale ereditano tutti i componenti dei nostri giochi
{
    void Start()
    {
        //Let's illustrate inheritance with the 
        
	     //default constructors.
        
        Debug.Log("Creating the fruit"); 
        
        Fruit myFruit = new Fruit();  
        
        Debug.Log("Creating the apple"); 
        
        Apple myApple = new Apple();  

        //Call the methods of the Fruit class.

        //Now let's illustrate inheritance with the 
        //constructors that read in a string.
        Debug.Log("Creating the fruit");
        
        myFruit = new Fruit("yellow");// crea un'istanza della classe Fruit.cs
        
        Debug.Log("Creating the apple");
        
        myApple = new Apple("green"); // crea un'istanza della classe Apple.cs

        //Call the methods of the Fruit class.
        myFruit.SayHello(); // scrive: Hello. I am a fruit.
        
        myFruit.Chop(); // scrive: The yellow fruit has been chopped

        //Call the methods of the Apple class.
        //Notice how class Apple has access to all
        //of the public methods of class Fruit.
        myApple.SayHello(); // scrive: Hello. I am a fruit.
        
        myApple.Chop(); // scrive: The green fruit has been chopped
    
     }

}

[/charp]

Fruit.cs



using UnityEngine;
using System.Collections;

//This is the base class which is
//also known as the Parent class.
public class Fruit 
{
    public string color;

    //This is the first constructor for the Fruit class
    //and is not inherited by any derived classes.
    public Fruit() // deve esserci obbligatoriamente il costruttore che riceve 0 parametri
    {
        color = "orange";
        Debug.Log("1st Fruit Constructor Called");
    }

    //This is the second constructor for the Fruit class
    //and is not inherited by any derived classes.
    public Fruit(string newColor)
    {
        color = newColor;
        Debug.Log("2nd Fruit Constructor Called");
    }

    public void Chop()
    {
        Debug.Log("The " + color + " fruit has been chopped.");
    }

    public void SayHello()
    {
        Debug.Log("Hello, I am a fruit.");
    }
}

Apple.cs


using UnityEngine;
using System.Collections;

//This is the derived class whis is
//also know as the Child class.
public class Apple : Fruit // Apple estende Fruit 
{
    //This is the first constructor for the Apple class.
    //It calls the parent constructor immediately, even
    //before it runs.
    public Apple() // deve esserci obbligatoriamente il costruttore che riceve 0 parametri
    {
        //Notice how Apple has access to the public variable
        //color, which is a part of the parent Fruit class.
        color = "red"; // la proprietà color è in Fruit.cs
        Debug.Log("1st Apple Constructor Called");
    }

    //This is the second constructor for the Apple class.
    //It specifies which parent constructor will be called
    //using the "base" keyword.
    public Apple(string newColor) : base(newColor)
    {
        //Notice how this constructor doesn't set the color
        //since the base constructor sets the color that
        //is passed as an argument.
        Debug.Log("2nd Apple Constructor Called");
    }
}


On console:

– Creating the fruit
– 1st Fruit Constructor Called

– Creating the apple
– 1st Fruit Constructor Called
– 1st Apple Constructor Called

– Creating the fruit
– 2nd Fruit Constructor Called

– Creating the apple
– 2nd Fruit Constructor Called
– 2nd Apple Constructor Called

– Hello, I am a fruit
– The yellow fruit has been chopped

– Hello, I am a fruit
– The green fruit has been chopped

For italian people:

1. Fruit myFruit = new Fruit(); -> public Fruit(), il costruttore senza parametri

2. Apple myApple = new Apple(); -> public Fruit() poi public Apple()

3. myFruit = new Fruit(“yellow”); -> public Fruit(string newColor)

4. myApple = new Apple(“green”); -> -> public Fruit(string newColor) poi public Apple(string newColor) : base(newColor) specifica la chiamata al costruttore di Fruit.cs tramite la keyword : base(newColor) – estende newColor

5. Fruit.cs scrive Debug.Log(“Hello, I am a fruit.”); e Debug.Log(“The ” + color + ” fruit has been chopped.”);

By |CSharp, Video Games Development|Commenti disabilitati su Unity Programming – Classes Inheritance (Ereditarietà) – Intermediate – CSharp

Unity Programming – Nested Classes – CSharp – Intermediate

How to access properties of a nested class from another script.
Accesso ad una proprietà all’interno di una classe annidata in uno script esterno.

Create an ‘Empty Object’ and attach the scripts:

– SomeClass.cs


using UnityEngine;
using System.Collections;

public class SomeClass : MonoBehaviour // deve essere MonoBehaviour anche questa o non funziona
{
    // proprietà della classe principale
    public int bullets = 10;

    // classe annidata
    
    [System.Serializable] // DEVO dichiarala Serializable o non si può accede da uno script esterno
    public class Something
    {
        // proprietà della classe annidata
        public int fuel = 15;
    }
    public Something Some; // la inserisco in una variabile
}

– SomeOtherClass.cs


using UnityEngine;
using System.Collections;

public class SomeOtherClass : MonoBehaviour
{
    void Start()
    {
        // il nome SomeClass è quello dello script SomeClass.cs
        SomeClass SomeClassScript = transform.GetComponent<SomeClass>();

        // variabile che contiene la classe principale . proprietà
        Debug.Log(SomeClassScript.bullets); // 10

                   // nomeScript.nomeVariabileClasse.proprietà
            Debug.Log(SomeClassScript.Some.fuel); // 15
    }
}

Come funziona?

1. SomeOtherClass.cs

– memorizza in una variabile lo script ottenuto con GetComponent
– richiama la proprietà della classe principale SomeClassScript.bullets
– richiama la proprietà della classe annidata SomeClassScript.Some.fuel

2. SomeClass.cs

– dichiaro l’accessibilità della classe dall’esterno serializzandola [System.Serializable]

– inserisco la classe in una variabile public Something Some
Posso notare che poi la chiamata sarà su – Some – SomeClassScript.Some.fuel

By |CSharp, Unity3D, Video Games Development|Commenti disabilitati su Unity Programming – Nested Classes – CSharp – Intermediate