lunes, 23 de mayo de 2011

sábado, 14 de febrero de 2009

autoloader personalizado para Zend Framework

Zend framework carga por defecto solo los archivos de las clases de la librería, para personalizar la carga de clases propias es necesario modificar el archivo index.php de la carpeta /zend/public y agregar un archivo cargador personalizado (que extiende el autoload por defecto del ZF)


Archivo /public/index.php

// public/index.php
//
// Step 1: APPLICATION_PATH is a constant pointing to our
// application/subdirectory. We use this to add our "library" directory
// to the include_path, so that PHP can find our Zend Framework classes.
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/'));
set_include_path(
APPLICATION_PATH . '/../library'
. PATH_SEPARATOR . get_include_path()
);

// Step 2: AUTOLOADER - Set up autoloading.
// This is a nifty trick that allows ZF to load classes automatically so
// that you don't have to litter your code with 'include' or 'require'
// statements.
require_once "Zend/Loader.php";

// Zend_Loader::registerAutoload(); comentado por Lix para agregar mi propio cargador de clases

/**
*
* Agregado por Lix:
* mi propio cargador de clases
*
*/

include(APPLICATION_PATH."/mi_Loader.php"); // Custom loader class
Zend_Loader::registerAutoload("mi_Loader");



// Step 3: REQUIRE APPLICATION BOOTSTRAP: Perform application-specific setup
// This allows you to setup the MVC environment to utilize. Later you
// can re-use this file for testing your applications.
// The try-catch block below demonstrates how to handle bootstrap
// exceptions. In this application, if defined a different
// APPLICATION_ENVIRONMENT other than 'production', we will output the
// exception and stack trace to the screen to aid in fixing the issue
try {
require '../application/bootstrap.php';
} catch (Exception $exception) {
echo '
'
. 'An exception occured while bootstrapping the application.';
if (defined('APPLICATION_ENVIRONMENT')
&& APPLICATION_ENVIRONMENT != 'production'
) {
echo '

' . $exception->getMessage() . '
'
. '
Stack Trace:'
. '
' . $exception->getTraceAsString() . '
';
}
echo '
';
exit(1);
}


// Step 4: DISPATCH: Dispatch the request using the front controller.
// The front controller is a singleton, and should be setup by now. We
// will grab an instance and call dispatch() on it, which dispatches the
// current request.
Zend_Controller_Front::getInstance()->dispatch();



Ahora el archivo mi_Loader.php




/**
*
* Archivo ubicado en /zend/application/mi_Loader.php
*
* se encarga de ampliar la rutina de autoloader, para permitir cargar
* clases de la forma categoria_NombreClase, donde los archivos
* correspondientes a esta clase se almacenan en la carpeta/archivo:
* /zend/library/categoria/NombreClase.php

class mi_Loader extends Zend_Loader
{

public static function loadClass($class, $dirs = null)
{
parent::loadClass($class, $dirs);
}

public static function autoload($class)
{
try {

// Ok, voy a cargar la clase: $class

if (substr($class, 0, 5)==='Zend_') {

// Aja, es una clase de Zend, echale bolas...
self::loadClass($class);

} else {

// Aja, es una clase mia... búscala en la carpeta de la aplicación, en library...
// y alli en la carpeta que indican los caracteres antes del "_"
// y el nombre del archivo de la clase son los caracteres que están depues del _

$clase_directorio = substr($class, 0, strpos($class, '_'));
$clase_nombre_archivo = substr($class, strpos($class, '_')+1);

self::loadClass($clase_nombre_archivo, APPLICATION_PATH . "/../library/" . $clase_directorio);
}
return $class;
} catch (Exception $e) {
return false;
}
}

}

.htaccess para Zend Framework

# archivo ubicado en /zend/public/.htaccess

# version del tutorial... no furula en ubuntu

# RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} -s [OR]
# RewriteCond %{REQUEST_FILENAME} -l [OR]
# RewriteCond %{REQUEST_FILENAME} -d
# RewriteRule ^.*$ - [NC,L]
# RewriteRule ^.*$ /index.php [NC,L]


# otra versión que si funciona en ubuntu, pero no furula la redireccion desde otros archivos hacia index.php

#
# RewriteEngine On
# RewriteBase /
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.php [L]
#

#

# mezcla de las dos versiones... si funciona en ubuntu y apache2


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /arte.php [NC,L]


# para que funcione apropiadamente en windows (realicé las pruebas con xampp)
# requiere descomentar la línea:
# LoadModule rewrite_module modules/mod_rewrite.so
# de httpd.conf

jueves, 23 de octubre de 2008

Reinicio de apache2

sudo /etc/init.d/apache2 restart

Instalación del soporte GD en php5

sudo apt-get install php5-gd

Instalación de mysql en ubuntu

apt-get install mysql-server mysql-client php5-mysql