ShiftSmith, a PHP Framework designed with an MVC architecture.

Business user

Business users section

Login at: http://dreamforgery.com/user

Developer

Developers section

Code Base at: github.com/LucLaverdure/ShiftSmith

Tutorials

Environment Setup — Copy, Extract, Build.

From Hello World to Pro ShiftSmither — A developer's guide

  • Controller Model View
    Think of the controller as the ignition firing after inserting the right key (trigger)

    /webapp/controllers/hello.php
    Think of the Model as data travelling from the controller to the view.

    $this->addModel('page', 'greeting', 'Hello world!');
    Think of the View as a skin or design envelopping the model.

    /webapp/views/default-theme/hello.tpl
  • We'll first be creating a controller.

  • Controllers are the ignition of all processes. If a class within the scanned files extends the class Controller, we're in business!

    Next ShiftSmith examines the validate function of the Controller.

    If validate return false, the controller is dropped.

    When validate returns a number, it represents the priority of execution (Controllers are sorted by highest priority first, keep in mind multiple controllers can trigger).

    Let's take a look at a controller.

  • /webapp/controllers/hello.php

    
    <?php
    
    // Hello World Example
    
    class helloworld extends Controller { // A controller is defined by "extends Controller"
    
    	// To determine if the controller is triggered, the validate function is used.
    	// returns anything but false to trigger
    	function validate() {
    		// Trigger home controller for /home and /
    		// the q() function returns the path requested without domain.
    		if (q()=="helloworld") {
    			// priority 1, more than 1 controller can be triggered,
    			// but we will prioritize this controller.
    			return 1;	
    		}
    		// if we are somewhere else than at /helloworld,
    		// do not trigger the controller.
    		else return false; 
    	}
    
    	// once the controller is triggered (it validated with the function above)
    	
    	// the execute function will pass the models to the view
    	function execute() {
    		// display view for hello world page (hello template)
    		$this->loadView('hello.tpl');
    	}
    }
    
    
  • Here is an example of the controller passing a static model "page.greeting"

    /webapp/controllers/hello.php

    
    [...]
    	// the execute function will pass the models to the view
    	function execute() {
    		// add hello world model
    		$this->addModel('page', 'greeting', 'Hello world!');
    		
    		// display view for hello world page (hello template)
    		$this->loadView('hello.tpl');
    	}
    [...]
    
    

    Here is an example of a view receiving a static model "page.greeting"

    /webapp/views/default-theme/hello.tpl

    
    <html>
    	<head></head>
    	<body>
    		<h1>[page.greeting]</h1>
    	</body>
    </html>
    
    

    Once rendered, will output Hello world!

  • Here is an example of a database fetch

    /webapp/controllers/hello.php

    
    [...]
    	// the execute function will pass the models to the view
    	function execute() {
    		
    		// Database preparation
    		$db = new Database();
    		$db::connect();
    		
    		// url pattern: chatbox/chatlog/room/last-post-id
    		//				  q(0) /  q(1) /q(2) q(3)
    		$room_id = q(2);
    		$last_post_id = (int) q(3);
    		
    		// use $db::param() to secure functions within the string
    		
    		// get all chat posts from main lobby
    		$chatlog = $db::queryResults("SELECT id, liner, md5(user) picture, user email
    								   FROM chatbox
    								   WHERE room_id='".$db::param($room_id)."'
    								   AND id > ".$last_post_id."
    								   ORDER BY id DESC
    								   LIMIT 50");		
    		
    		// add chatlog model
    		$this->addModel('chatlog', $chatlog);
    		
    		// display view for hello world page (hello template)
    		$this->loadView('hello.tpl');
    	}
    [...]
    
    

    And Here is an example of a view receiving a database model "chatlog".

    /webapp/views/default-theme/hello.tpl

    
    <html>
    	<head></head>
    	<body>
    		<h1>[page.greeting]</h1>
    		[for:chatlog]
    			 <p>[chatlog.email]: <pre>[chatlog.liner]</pre>
    		[end:chatlog]
    	</body>
    </html>
    
    

    Once rendered, will output a line per result.

  • Now it gets interesting, the injection scripts!

  • 
    [...]
    	function validate() {
    		// execute on all pages,
    		// priority of 15
    		// to let the page controllers execute before this one.
    		return 15; 
    	}
    
    	// once the controller is triggered
    	// (it validated with the function above)
    	function execute() {
    		// after the page controller is loaded.
    		// browse DOM to "header" id,
    		// then element with class "menu"
    		// and prepend the view menu.tpl
    		$this->injectView('#header .menu', 'prepend', 'menu.tpl');
    	}
    [...]
    
    

Continue >>

Documentation

@chatbox+