DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Automating Zend MVC Structure

03.17.2008
| 6368 views |
  • submit to reddit
        Automating Zend MVC Structure
This is what I use... It's very useful.


<?php
#
# Automate the creation of the Zend MVC Framework
# Script Written by: Fernando Barajas
# Usage: php ./zendcreate.php
#

# Create the Zend MVC Framework.
define('ZEND_FILE_TYPE', '.tar.gz');
define('ZEND_FILE_NAME', 'ZendFramework-1.5.0'. ZEND_FILE_TYPE);
define('ZEND_FRAMEWORK_URL', 'http://framework.zend.com/releases/ZendFramework-1.5.0/'. ZEND_FILE_NAME);
define('APPLICATION_NAME', 'zend_'. time());


passthru("wget ". ZEND_FRAMEWORK_URL);


if(file_exists(ZEND_FILE_NAME)) {
	# unzip the file -- tar zxvf ./ZEND_FILE_NAME
	passthru("tar zxvf ". ZEND_FILE_NAME);
	
	# Make the Applications Directories
	passthru("mkdir ". APPLICATION_NAME);
	
	# Move the Library directory into the application directory
	passthru("mv ./". basename(ZEND_FILE_NAME, ZEND_FILE_TYPE) ."/library ./". APPLICATION_NAME ."/library");
	
	# Make the App Directory 
	passthru("mkdir ". APPLICATION_NAME ."/app");
	
		passthru("mkdir ". APPLICATION_NAME ."/app/controllers");
		passthru("mkdir ". APPLICATION_NAME ."/app/models");
		passthru("mkdir ". APPLICATION_NAME ."/app/views");
			
			passthru("mkdir ". APPLICATION_NAME ."/app/views/scripts");
			
	
	# Make the public Directory
	passthru("mkdir ". APPLICATION_NAME ."/public");
	
	# Make the Front Controller / Bootstrap file
$zend_bootstrap_code = '
<?php
# Set Error Reporting
error_reporting(E_ALL|E_STRICT);
ini_set(\'display_errors\', \'on\');

# Set include path the DOCUMENT_ROOT / library for Zend Library
ini_set(\'include_path\', $_SERVER[\'DOCUMENT_ROOT\']."/../library");

# Zend Framework Includes
require_once \'Zend/Loader.php\';

Zend_Loader::loadClass(\'Zend_Controller_Front\');

# Un-Comment the two lines below if you will be using Routes
# Most likely you will be. I recommend that you do. 
# They are very useful
#
# Zend_Loader::loadClass(\'Zend_Controller_Router_Rewrite\');
# Zend_Loader::loadClass(\'Zend_Controller_Router_Route_Regex\');
#

# Get the front controller instance
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory("../app/controllers");

$front->throwExceptions(true);

#
# Get an Instance For Routing
# $route = $front->getRouter(); // Returns a rewrite router by default
# $route->addRoute(\'user\',
#	new Zend_Controller_Router_Route(\'user/:username\', array(\'controller\' => \'index\', \'action\' => \'test\'))
# );
#
# $route->addRoute(\'product\',
#	new Zend_Controller_Router_Route_Regex(\'product/(\d+)\', array(\'controller\' => \'index\', \'action\' => \'test\'))
# );
#
# $route->addRoute(\'login\',
#	new Zend_Controller_Router_Route_Regex(\'login\', array(\'controller\' => \'index\', \'action\' => \'test\'))
# );
#

# Start the dispatch
$front->dispatch();

?>
';
	# Write the Bootstrap file
	file_put_contents("./". APPLICATION_NAME ."/public/index.php", trim($zend_bootstrap_code));
	
# Write The Index Controller
$zend_index_controller_code = '
<?php

class IndexController extends Zend_Controller_Action {

	public function indexAction() {
		# => Logic code here...
		$this->view->random_number = rand(0,10);
		$this->view->status = "It works!";
		$this->render("index");
	}
}

?>
';
	# Write the First Index Controller
	file_put_contents("./". APPLICATION_NAME ."/app/controllers/IndexController.php", trim($zend_index_controller_code));


# Write the index view file
$zend_index_view_code = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>Zend Framework</title>
</head>
<body>
<h2>Status: <?= $this->status ?></h2>
<div>
	Random Number: <?= $this->random_number ?>
</div>
</body>
</html>
';
	# Write the First Index View File
	passthru("mkdir ./". APPLICATION_NAME ."/app/views/scripts/index");
	file_put_contents("./". APPLICATION_NAME ."/app/views/scripts/index/index.phtml", $zend_index_view_code);
	
	
# Create the .htaccess file
$zend_htaccess_code = '
RewriteEngine On
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
';
	# Write the .htaccess code to disk
	file_put_contents("./". APPLICATION_NAME ."/public/.htaccess", trim($zend_htaccess_code));
	
	
	# Delete the Downloaded Framework Directory and tar file
	passthru("rm -f ./". ZEND_FILE_NAME);
	passthru("rm -rf ./". basename(ZEND_FILE_NAME, ZEND_FILE_TYPE));
	
	passthru("clear");
	
	echo "\nZend Framework has been created... Done!\n\n";
}

?>