« Older Entries Newer Entries » Subscribe to Ultimi articoli

9 mag 2008

links for 2008-05-09

Nessun commento


8 mag 2008

links for 2008-05-08

Nessun commento


6 mag 2008

links for 2008-05-06

Nessun commento


4 mag 2008

links for 2008-05-04

Nessun commento

3 mag 2008

links for 2008-05-03

Nessun commento

2 mag 2008

CakePHP e i layout (2)

Nessun commento

Che cosa sono i layout in CakePHP ed a cosa servono?

Essenzialmente i layout sono dei file contenenti codice di presentazione che definisce come mostrare i dati delle views. Ogni view quindi dovrebbe essere visualizzata all’interno di un layout.

E’ importante ora notare che CakePHP è disponibile attualmente in due versioni:

  • Stable: 1.1.19.6305
  • Beta: 1.2.0.6311

Queste due versioni si differenziano tra le altre cose per il fatto che la prima versione contiene solo i layout default, ajax e flash mentre la versione 1.2 Beta contiene i layout default, ajax, flash, xml, js, e rss.

Questi layout sono già all’interno di CakePHP e si trovano nella cartella /cake/libs/view/templates/layouts.

Quanto segue fa sempre riferimento alla versione Stable 1.1.19.6305 di CakePHP (la versione 1.2 Beta usa l’estensione ctp per i layout invece dell’estensione thtml usata nella 1.1.19.6305).

All’interno della classe Controller alla riga 142 viene specificato come layout quello di default, che si trova nella cartella /app/views/layouts/default.thtml. Pertanto se vogliamo usare un layout creato da noi per tutte le pagine basterà modificare il file default.thtml. Se volessimo ripristinare un giorno il layout di default di CakePHP basterà ricopiarlo dalla cartella /cake/libs/view/templates/layouts.

Quando si crea un nuovo layout è necessario indicare a CakePHP dove inserire il codice generato dalle views. Per fare questo si inserisce questa riga:

$content_for_layout //and optionally $title_for_layout

Per esempio andiamo a vedere il layout ajax, già presente in entrambe le versioni di CakePHP:

 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *			1785 E. Sahara Avenue, Suite 490-204
 *			Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.cake.libs.view.templates.layouts
 * @since			CakePHP(tm) v 0.10.0.1076
 * @version			$Revision: 6305 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2008-01-01 20:33:56 -0600 (Tue, 01 Jan 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
?>

Come si può notare il layout ajax non fa altro che stampare esattamente i dati generati dalla view, risulta pertanto utile nel caso analizzato in precedenza.

Non c’è limite al infine al numero di layout che si possono creare, basta salvarli nella cartella /app/views/layouts (dove app è il nome dell’applicazione di CakePHP) e specificare all’interno del controller di usare il layout desiderato, con la seguente funzione (immaginando di aver creato un file my_layout.thtml ed averlo salvato nella cartella appena citata):

// inside your controller file
$this->layout = 'my_layout';  // you can use setLayout() too

Per completezza riporto qui sotto il codice degli altri due layout predefiniti di CakePHP, ovvero default e flash:

 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.cake.libs.view.templates.pages
 * @since			CakePHP(tm) v 0.10.0.1076
 * @version			$Revision: 6305 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2008-01-01 20:33:56 -0600 (Tue, 01 Jan 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
?>
 
charset(); ?>
 
css('cake.generic'); ?>
<div id="container">
<div id="header">
<h1>CakePHP Rapid Development</h1>
</div>
<div id="content">
			check('Message.flash'))
					{
						$session-&gt;flash();
					}
					echo $content_for_layout;
			?&gt;</div>
<div id="footer">
 
			<a href="http://www.cakephp.org/" target="_new">
				image('cake.power.png', array('alt'=&gt;"CakePHP(tm) : Rapid Development Framework", 'border'=&gt;"0")); ?&gt;
			</a></div>
</div>
 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.cake.libs.view.templates.layouts
 * @since			CakePHP(tm) v 0.10.0.1076
 * @version			$Revision: 6305 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2008-01-01 20:33:56 -0600 (Tue, 01 Jan 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
?&gt;
 
charset(); ?&gt;
 
<!--
P { text-align:center; font:bold 1.1em sans-serif }
A { color:#444; text-decoration:none }
A:HOVER { text-decoration: underline; color:#44E }
-->
 
<a href="&lt;?php echo $url; ?&gt;"></a>

Tags:

2 mag 2008

CakePHP e i layout (1)

3 Commenti

Qualche tempo fa scrivevo che stavo cercando di capire come mai lo stesso codice si comportava in maniera diversa se caricato in locale (sul mio computer) o in remoto (sullo spazio web di netsons).

In particolare il problema era il seguente:
creando un nuovo post (da qui) oppure modificandone uno già presente, compariva la finestra popup con un messaggio “SAVE FAILED” nonostante il salvataggio fosse andato a buon fine.

Come si spiega tutto ciò?

Andando a rivedere passo passo il codice sviluppato (seguendo questo tutorial già citato precedentemente) mi sono soffermato su queste due righe:

// functions edit2 and add2 in posts_controller.php
Configure::write('debug', '0');     //turn debugging off; debugging breaks ajax
$this-&gt;layout = 'Ajax';     //set the layout to Ajax so the ajax doesn't break

Vediamo cosa fanno esattamente:

  1. Configure::write imposta la variabile DEBUG nel file di configurazione a 0. Se andiamo a vedere il file core.php situato in app2/config/ (dove app2 è il nome della mia applicazione in CakePHP) vediamo i vari livelli di debug:
    /*** Set debug level here:
     
    * - 0: production
     
    * - 1: development
     
    * - 2: full debug with sql
     
    * - 3: full debug with sql and dump of the current object
     
    *
     
    * In production, the "flash messages" redirect after a time interval.
     
    * With the other debug levels you get to click the "flash message" to continue.
     
    *
     
    */
     
    define('DEBUG', 1);
  2. $this->layout = ‘Ajax’; invece imposta il layout di nome Ajax che è tra quelli predefiniti di CakePHP.

Dove stava quindi l’errore?

Tutto dipendeva dal fatto che Windows NON è case-sensitive mentre Linux sì!

In pratica, scrivendo “Ajax” invece di “ajax” sotto Windows (dove stavo effettuando le mie prove in locale) funzionava ugualmente, sotto Linux (su cui si trova netsons) invece no!

Quando scrivete codice quindi – questa è la lezione che ho imparato dopo tante sofferenze – state MOLTO attenti alle maiuscole ed alle minuscole! 😉

Per una spiegazione più dettagliata riguardo CakePHP e i layout rimando a questo mio articolo.

Tags: , , ,

2 mag 2008

links for 2008-05-02

Nessun commento

2 mag 2008

Update ad Ubuntu 8.04 LTS

Nessun commento

Ho appena finito di aggiornare il mio sistema operativo linux all’ultima versione di Ubuntu ma è stato più difficile del previsto. Dopo circa 5 ore tra scaricamento ed installazione pacchetti, mi trovo davanti una schermata nera!!!

Grazie a Napo che mi ha aiutato ad installare la famigerata scheda NVIDIA così:

  1. all’avvio, quando compare la schermata nera, digitare CTRL+ALT+F2 per andare in console
  2. digitare sudo nano /etc/X11/xorg.conf (sostituite nano con vim se preferite!)
  3. nella sezione Section “Device” sostituire  la riga Driver         “nvidia” con Driver         “nv
    (oppure “vesa” se “nv” non va ugualmente)
  4. salvare il file (digitando :wq se usate vim)
  5. fare ripartire il GDM (Gnome Display Manager) digitando sudo /etc/init.d/gdt restart

Dopo aver sistemato la scheda grafica, per impostare la risoluzione a 1280×1024 (che non compariva tra quelle disponibili) ho modificato sempre il file xorg.conf nella sezione Section “Screen” sostituendo Monitor        “default” con Monitor        “Acer AL801” ed ho potuto quindi selezionarla tranquillamente.

La tastiera poi era impostata con layout inglese (USA), ma andando sotto Sistema->Preferenze->Tastiera ho facilmente sistemato modificando la disposizione a “italiana”.

Infine avevo problemi con la scheda di rete (ne ho due installate) che non si connetteva alla LAN (cliccando su ETH0 ricevevo un errore “L’interfaccia ETH0 non esiste!”), ma riavviando un paio di volte tutto si è risolto pare (non ho ancora capito bene come!).

Tags:

1 mag 2008

links for 2008-05-01

Nessun commento

  • Ricerca

    or
  • Language

  • Categorie

  • Delicious

  • Statistiche


    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081
    • Pages displayed : 33362
    • Unique visitors : 18465
    • Pages displayed in last 24 hours : 0
    • Unique visitors in last 24 hours : 0