Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Automating Zend MVC Structure

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";
}

?>

Very simple script using Dojo widget and I/O

Dojo is a Java Script toolkit. This is a minimal HTML page which uses a widget and talks to the server. This is based on the Dojo tutorial at JotSpot.

<html>
<head>
  <title>Dojo: Hello World!</title>

  <script type="text/javascript" src="/js/dojo/dojo.js"></script>

  <script type="text/javascript">
    dojo.require("dojo.event.*");
    dojo.require("dojo.widget.*");
    dojo.require("dojo.widget.Button");

    function helloPressed()
    {
        dojo.io.bind({
                       url: '/cgi-bin/HelloWorldResponsePOST.rb',
                       handler: helloCallback,
                       formNode: dojo.byId('myForm')
                    });
    }

    function init()
    {
      var helloButton = dojo.widget.byId('helloButton');
      dojo.event.connect(helloButton, 'onClick', 'helloPressed')
    }

    function errorCallback(type, error)
    {
        alert(error)
    }

    function helloCallback(type, data, evt)
    {
      if (type == 'error')
        alert('Error when retrieving data from the server!');
      else
        alert(data);
    }

    dojo.addOnLoad(init);
  </script>
</head>

<body>
  <button dojoType="Button" widgetId="helloButton">Hello World!</button>
  <br>

  <form id="myForm" method="POST">
    Please enter your name: <input type="text" name="name">
  </form>

</body>
</html>

The Camping Short, Short Example

Camping encourages short, elegant applications. In this example, we're going to skip the database and put together a simple home page with a few of your favorite links.

The site can be stored in a single file called home_page.rb:

 #!/usr/local/bin/ruby -rubygems
 require 'camping'

 module Camping::Controllers

   # The root slash shows the `index' view.
   class Index < R '/'
     def get
       render :index 
     end
   end

   # Any other page name gets sent to the view
   # of the same name.
   #
   #   /index -> Views#index
   #   /sample -> Views#sample
   #
   class Page < R '/(\w+)'
     def get(page_name)
       render page_name
     end
   end

 end

 module Camping::Views

   # If you have a `layout' method like this, it
   # will wrap the HTML in the other methods.  The
   # `self << yield' is where the HTML is inserted.
   def layout
     html do
       title { 'My HomePage' }
       body { self << yield }
     end
   end

   # The `index' view.  Inside your views, you express
   # the HTML in Ruby.  See http://code.whytheluckystiff.net/markaby/.
   def index
     p 'Hi my name is Charles.'
     p 'Here are some links:'
     ul do
      li { a 'Google', :href => 'http://google.com' }
      li { a 'A sample page', :href => '/sample' }
     end
   end

   # The `sample' view.
   def sample
     p 'A sample page'
   end
 end

 if __FILE__ == $0
   puts Camping.run
 end


Source: The Camping Short, Short Example
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS