PHP code example of voodoophp / voodoo

1. Go to this page and download the library: Download voodoophp/voodoo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

voodoophp / voodoo example snippets



	    /**
     * Set the application name to use. By default it's Www
     * @type string
     */
    $appName = "www";

    (new Voodoo\Core\Application(APP_ROOT_DIR, $appName))->doVoodoo();


Everythings comes here first then go to where they need to be


### Front-Controller serving multiple application based on URL

The example below illustrates how you can have multi-sites using the same code base. 

Based on the hostname, each site will use a different application directory. You will have to point all the domains to the same IP address for it to work.  

Let's use the /App/_conf/app.json above for our multi-sites

/index.php

	

	    $hostName = Voodoo\Core\Env::getHostName();
    switch($hostName) {
        case "api.mysite.com" :
            $appName = "Api";
            break;
        case "crazyapps.com" :
            $appName = "AnotherApp";
            break;
        case "site1.com" :
            $appName = "Site1";
            break;
        default: // mydefaultsite.com
            $appName = "Www";
    }
    
    (new Voodoo\Core\Application(APP_ROOT_DIR, $appName))->doVoodoo();

So now you have a multi-sites application. Ayibobo!

---

## Smart Routing & Application Execution

Voodoo cleverly routes to the right module of the application by using the URL schema

		site.com/Module/Controller/Action/Segments/?paramName=paramValue

* Module: The container of the MVC application

* Controller: A class accepting the user request

* Action: A method of the controller's class

* Segments: extra parameters that are passed in the action

* ParamName/ParamValue : Parameters from the query url

### How does Voodoo excute your application?

The very first thing Voodoo he application will be re-routed to `/Users/Profile/Info/mardix`.

You can add as many routes as you want. Also, the routes can have as many directives as you want and they support regex for more advanced matching.

	path["/profile/(:any)"] = "/Users/Profile/Info/$1"
	path["/music/([rap|techno|compas]+)/(:num)"] = "/Music/Selection/genre/$1/song/$2"
	path["/blog/(:alnum)/(:num)/(:any)"] = "Main/Blog/Category/$1/post/$2/$3/"
	path["(:any)"] = "$1"

The /music route: site.com/music/rap/1526 -> /Music/Selection/genre/{rap}/song/{1526}
	
	Music = module
	Selection = controller
	genre = action
	rap = segement #1, accessed in the controller $this->getSegment(1)
	song = segement #2, accessed in the controller $this->getSegment(2)
	1526 = segement #3, accessed in the controller $this->getSegment(3)
		

#### Stuff to understand about routes:

- To reduce overhead, Application's routes are run before the call to any MVC application

- Re-routing is not a redirect. It routes a path to a new path without changing the URL in the address bar

- Voodoo routes are case incensitive. So accessing */Profile/info/mardix* and */pRofile/inFo/mardix* will result to the same place

- Only alphanumeric [AZ-09] characters are accepted. All the other one will be ignored when routing. So */pro-file/info/mardix* will still result to */Profile/Info/mardix/*. Same as */about-us/* which result to */aboutus/*. So you can cleverly write your SEO friendly url and still keep your application running with no headache.


*It is important to understand that routes are matched in the order they are added, and as soon as a URL matches a route, routing is essentially "stopped" and the remaining routes are never tried. Because the default route matches almost anything, including an empty url, new routes must be place before it.*

	path[/article/(:num)] = "/Blog/Article/Read/$1"
	path["/profile/(:any)"] = "/Main/Profile/Info/$1"
	path["(:any)"] = "$1"

---

Now you see how Voodoo works with Front-Controller and Routes, let's get in the sauce of the meat. Ayibobo!

---

## Model-View-Controller

As you already notice above, your MVC application will reside under an $AppName and a $ModuleName.

- Models: `App\$AppName\$ModuleName\Model`

- Controllers: `App\$AppName\$ModuleName\Controller`

- Views: `App/$AppName/$ModuleName/Views`


Let's say we are using the `Www` application and the `Main` module.


**Controller**

All controllers will reside under the namespace: `App\Www\Main\Controller`

App/Www/Main/Controller/Index.php

	

	namespace App\Www\Main\Controller;

	use Voodoo;

	class Index extends BaseController
	{

	    public function actionIndex()
	    {
			$this->view()->setTitle("Hello World");
	    }


	    public function actionAboutUs()
	    {

	    }
		//...

	}

All controllers are extended by `BaseController` which is extended by `Voodoo\Core\Controller` which contains the methods needed to get params, load views, get models, etc... BaseController is set so you can share functionnalities with other controllers

**Views**

All views a placed at: `App/Www/Main/Views`

Views, by default, are HTML template files. Based on the separation of concerns principle, the views role are to display data. No logic whatsoevet. To accomplish that, Voodoo uses *[Mustache.php](https://github.com/bobthecow/mustache.php/tree/cd6bfaefd30e13082780b3711c2aa4b92d146b1e)* as the template engine to render HTML. 

Other engines, such as Twig can be used instead of Mustache. 

But if you are thinking about using PHP as template system itself, like  `<title><?= $this->title;