Switch board (framework)
From Wikipedia, the free encyclopedia
Switch Board is a MVC framework written for PHP. Its original concepts were taken from a preexisting ColdFusion/PHP framework called Fusebox. Its evolution through development has greatly changed its structure to have very little resemblance to Fusebox other than slight functionality of the switch, settings and template files.
Contents |
[edit] Description
The current release of Switch Board is Switch Board with Routing 2.0.1. This version was released on December 25th, 2006 by the developer Daniel Slaughter.
Switch Board has three sub-directories from its root. These sub-directories are the control, model and view. When a page is called from a Browser the PHP Server executes the control, model and then view file before displaying the resulting content to the called Browser. This separation of MVC is seamless to the end user.
- Core
The core file is the brains behind everything. This file (sb_core.php) consists of 66 lines of code. The core file executes in the following manner:
- 1. The URL is parsed to determine what circuit and page to gather the control/model/view pages from.
- 2. The Settings file is then included which contains any global variables. If a circuit other than the root was found in the URL then its settings file is as well included here.
- 3. The Switch file from the root or circuit is included (but not both).
- 4. PHP determines if control/page.php exists and includes it (where page equals the file the browser is attempting to access)
- 5. PHP determines if model/page.php exists and includes it
- 6. PHP determines if view/page.php exists and includes it
- 7. Any content generated from the control/model/view is then embedded in the template and displayed to the Browser. If no content was generated, the designated error page is included instead (specified in the settings file).
- Settings
The settings is held in a file called sb_settings.php in the root of the Switch Board files. This file is where any global variables are held. For instance, the configuration variables of Switch Board are located here. You may add any additional variables you wish to this file that your application will use. The settings file are called prior to any other file from the sb_core.php file.
- Switch
The switch is held in a file called sb_switch.php in the root of the Switch Board files. This file is a simple PHP switch-case that handles any global variables to the individual page (such as the page title, if the page redirects, and exit actions: links from this page). Please note: you do not need to have include statements for the control/model/view files here. Switch Board assumes if they exist they should be included (this greatly increases development time and ensures file naming schemes). The switch file is called before the control/model/view files are included.
- Control
Processes and responds to events, typically user actions, and may invoke changes on the model. In Switch Board the control is meant to make inserts/updates to a database or other file structure. These pages are typically called from the posting of a form.
- Model
The model is another name for the domain layer. The model in Switch Board is where database select statements are pooled. Calculations can also be done here to data before it is presented to the view file for display.
- View
Renders the model into a form suitable for interaction, typically a user interface element. In Switch Board the view page contains the display generated from the data retrieved in the model. Only the information physically presented to the Browser should be held here (such as XHTML, HTML, CSS). However, for debugging purposes content can be displayed to the Browser through the control and model but is not suggested for final release.
- Template
The template is held in a file called sb_template.php in the root of the Switch Board files. This template file contains the wrapper XHTML/HTML for the content generated from a page call to Switch Board from the Browser. The template is the last file to include. There are parameters you can use to ignore the template (ie: $sb['useTemplate'] = false).
[edit] Advantages of Switch Board
- Switch Board gets away from the Object-oriented programming approach to MVC Frameworks.
- The current core file of Switch Board contains 66 lines of code.
- No configuration is required to add a page. Switch Board includes files in the /control/, /model/ and /view/ folder (in that order) if they exist with the same name as the routed file called. So, if /helloworld.htm was called, /control/helloworld.php, /model/helloworld.php, and /view/helloworld.php would be included if they exist.
- A global sb_settings.php file is used to define global variables like those used in the configuration of Switch Board.
- Any content generated is embedded into the sb_template.php file for display. If a circuit is called, its content it generated within its template, and then generated within the root's template.
- Switch Board is very fast to develop in because no additional configuration is required in the switch to add a page. A simple Hello World application would require you to create a file called helloworld.php in the /view/ folder from the root with the following text:
Hello World
You can then access this Hello World application by going to an address such as: http://domain.com/helloworld.htm
[edit] Built in capabilities
- Simple redirecting is a built in feature which allows a developer to specify to Switch Board what page to redirect to after processing the controller file:
$sb['redirect'] = "http://domain.com/control/page.htm";
- Authentication allows a developer to restrict page access to only those guests who have a session variable, defined in the sb_settings.php file:
// turn on/off authentication $sb['useAuthentication'] = true // what variable to check if it exists (such as a session that only exists if they're "logged in") $sb['hasAuthentication'] = isSet($_SESSION['auth']); // the array of pages you should only be able to access without authentication: include circuitpath/extension $sb['noAuthentication'] = array("index.htm","apple.htm"); // you will be redirected here if you attempt to access an internal page that you do not have authentication to $sb['noAuthenticationRedirect'] = "/sb_20/index.htm?error=authentication"; // if you are authenticated and attempt to access a non-authenticated page, redirect to here (such as the login page) $sb['authenticationRedirect'] = "/sb_20/hasauthentication.htm";
- Enabling or disabling the template, control, model, or view call:
$sb['useTemplate'] = true; $sb['useControl'] = true; $sb['useModel'] = true; $sb['useView'] = true;
- Custom error page (404: "file" not found):
// this page will be included if someone types in an incorrect page address. $sb['errorPage'] = "error404";
- Personalization of routing file appearance in both the file extension and default page:
$sb['fileExt'] = ".htm"; $sb['defaultPage'] = "index";
[edit] External links
[edit] References
Wikipedia contributors, "Model-view-controller," Wikipedia, The Free Encyclopedia, (accessed December 26, 2006).