This is the "get up and running in 5 minutes" documentation, at this point in time, don't expect anything more. If you have specific questions, don't hesitate to email me. Or better yet, ask them as "issues" in the crumb-mvc Google Code project.
To install Crumb, simply download the package, unzip it, and move it to the location where you wish to develop your application.
Be sure to move .htaccess when you are moving the contents of crumb.zip to their new location. Without .htaccess your mileage will vary. As in vary low.
Model-view-controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from the user interface, permitting one to be freely modified without affecting the other. The controller collects user input, the model manipulates application data, and the view presents results to the user. Typically, views and controllers come in pairs, each corresponding to a small part of the user interface; an application will likely be built with many pairs. - via Wikipedia
In summary, you're going to have three different folders: /models, /views, and /controllers, each of these will have MVC files in them. For example, the controller "lemonade" should reside in /controllers/lemonade.php.
Unlike controllers, models and views can be stored in sub-directories of their enclosing folder. The model "admins" could reside in /models/users/admins.php.
To get started, open up index.php and take note of all the configuration variables. The figure on the left coincides.
This is the basic structure of a Crumb application. All the files in the application are stored somewhere, be that your HDD, or a webserver.
Controllers are the core element of Crumb. Controller class files are to be stored in /controllers with the same filename as their classname. All controllers must extend the class "controller."
Example: the controller "hello_world" would exist in /controllers/hello_wold.php, and be defined as "class hello_world {"
If you wanted to access a controller in your browser, you would navigate to BASE_URL/controller_name. For example, the controller name of this page is "documentation", the BASE_URL is "crumb", as the root is www.breefield.com.
You can add an action to your controller simply by defining a method for that controller. The method defined for this page is __index, as I have DEFAULT_ACTION set to "__index". If you wanted to call the action "help", you would navigate to "www.breefield.com/crumb/documentation/help", which in turn would call the method "help" on the class "documentation."
Controllers interact with models and views by calling them up.
Views are shown by calling "$this->show('view_name');" inside the action (method) that is being called in your controller.
And models are called by calling "$this->load('model_name);."
The contents of /controllers/authentication.php
class authentication extends controller {
public function login($name='') {
$this->load->('user');
$vars['success'] = $this->user->login($name[0]);
$this->show('authentication/login', $vars);
}
}
By visiting the URI /authentication/login we would be presented with the view /views/authentication/login.php. Prior to that the model /models/user.php would have been loaded and instantiated as "$this->user."
Something else I haven't touched upon, when defining the controller "login" I added the parameter "$name." This parameter is filled by passing variables in the URI.
By visiting /authentication/login:Dustin you would populate that parameter with an array: "Array ( [0] => Dustin )", if you were to visit /authentication/login:name=Dustin, you would populate the $name parameter with the array "Array ( [name] => Dustin )"
You can pass as many parameters on the back of the URI as you like, simply delimitate them with /'s,
i.e. /authentication/login:name=Dustin/age=17
Models are dead simple. Simply create a class that extends the class "model.". This class and it's methods are in charge of retrieving data from a your database, doing data calculations, etc.
Here is an example of the model "user", stored in /models/user.php
class user extends model {
public function login($email, $password) {
if(!empty($email) && !empty($password)) return true;
else return false;
}
}
Views are the easiest piece to write, as there is no class definition required to create a view. Simply call views in your
controller via "$this->show('view_name', $vars);"
The variable $vars should be an array (or multidimensional array) where the keys are the variable names you wish to use in your view.
For example, your controller calls up a view like so.
$vars['success'] = 'maybe if you ask nicely';
$this->show('authentication/login', $vars);
And /views/authentication/login.php's contents
<h1>Hello there</h1>
<br/>
Was it a success?
<br/>
<?= $success; >
In order to path back to the root of your application, just call <?= root(); ?> which will output the app's base url so you can link to something from the root of your application.
There might be a few views you want to show on every page, like a header and a footer, etc. You can do this with Crumb. In your controller just define the property $layout_top and $layout_bottom like so...
class authentication extends controller {
public $layout_top = array('core/header');
public $layout_bottom = array('core/footer');
public function __layout() {
$this->load('user');
$this->layout_vars['logged'] = $this->user->isLogged();
}
public function login($name='') {
...
}
}
The property $layout_vars will be passed when calling the layout views. The layout views will be able to access $logged above just like any other view.
Lastly, take a look at /config/routes.php.
To create a route, just call routing::addRoute('alias', 'route');
For example, say you have the controller "users" and the action "login", but you want your URI to simply be /login.
You'd add the route like so...
routing::addRoute('login', 'users/login');
routing::addRoute('hey/there/homedawg', 'users/login');
Both routes will load the controller "users" and call the action "login."