/lynn/packages/controller.php
PHP | 169 lines | 87 code | 23 blank | 59 comment | 28 complexity | 015b3fe7ec680f5414c564e5a709bbc7 MD5 | raw file
- <?php
- /**
- * Lynn PHP
- *
- * @category Lynn PHP
- * @package Lynn_Controller
- * @version 1.0
- * @author Matt Lynn
- * @copyright 2009 - 2010 Matthew James Lynn
- * @license See included LICENSE.txt file
- */
-
- lynn_load('lynn_view');
- lynn_load('lynn_http');
-
- /**
- * Gets a url to a controller action.
- *
- * @param $base The base url in the format controller/action.format
- * @param $params The array of parameters to pass to the controller/action
- * @return The completed url.
- */
- function lynn_mvc_href($base='', $params=null) {
- if($params != null) {
- $str = '';
- foreach($params as $key => $value) {
- $str .= '/' . $key . ':' . urlencode($value);
- }
- $params = $str;
- }
-
- return LYNN_URL_ROOT . $base . $params;
- }
-
- /**
- * Redirects the user to a mvc action.
- *
- * @access public
- * @param string $href The location to direct the user to in the format 'controller/action'
- * @param string $params The parameters to pass to that action in the url
- */
- function lynn_mvc_redirect($href, $params=null) {
- lynn_http_redirect(lynn_mvc_href($href, $params));
- }
-
- /**
- * Provides the base controller object.
- */
- class Lynn_Controller {
- /**
- * The theme that the mvc should use
- *
- * @var string
- */
- public $theme = 'default';
-
- /**
- * The configuration passed to the Lynn_Controller_Front.
- *
- * @var array
- */
- public $config;
-
- /**
- * This method is called to run an action inside of the controller object.
- * It is recommended you do not, but you can override this to make certain
- * controllers behave dramatically differently.
- *
- * @param $action The name of the action to run.
- */
- public function dispatch($action) {
- if(method_exists($this, 'onReady')) {
- $this->onReady();
- }
-
- if(method_exists($this, 'before_filter')) {
- $this->before_filter();
- }
-
- $action = $action . '_action';
-
- if(method_exists($this, $action)) {
- $this->$action();
-
- } else if(method_exists($this, 'catch_all')) {
- $this->catch_all();
-
- } else {
- require LYNN_APP_ROOT . 'application/errors/404.html';
- }
-
- if(method_exists($this, 'after_filter')) {
- $this->after_filter();
- }
- }
-
- /**
- * Loads a resource such as a lynn_form or lynn_model.
- *
- * @param string $name The name of the class to load.
- */
- public function getResource($name) {
- // Load A Form
- if(str_replace('_form', '', $name) != $name) {
- if(!class_exists($name)) {
- $file = $this->config['paths.forms'] . str_replace('_form', '.form.php', $name);
- if(!file_exists($file)) {
- lynn_display_error('Form not found!', 'Requested form not found in file ' . $file);
- } else {
- require $file;
- }
- }
-
- return new $name();
- }
-
- // Load a Model
- else if(str_replace('_model', '', $name) != $name) {
- if(!class_exists($name)) {
- $file = LYNN_APP_ROOT . 'application/models/' . str_replace('_model', '.model.php', $name);
- if(!file_exists($file)) {
- lynn_display_error('Model not found!', 'Requested model not found in file ' . $file);
- } else {
- require $file;
- }
- }
-
- $model = new $name();
- $model->db &= $this->db;
- if(method_exists($model, 'onReady')) {
- $model->onReady();
- }
-
- return $model;
- }
-
- // Load a Helper
- else if(str_replace('_helper', '', $name) != $name) {
- if(!class_exists($name)) {
- $file = LYNN_APP_ROOT . 'application/helpers/' . str_replace('_helper', '.helper.php', $name);
- if(!file_exists($file)) {
- lynn_display_error('Helper not found!', 'Requested helper not found in file ' . $file);
- } else {
- require $file;
- }
- }
-
- return new $name();
- }
-
- else {
- return $this->bootstrap->getResource($name);
- }
- }
-
- /**
- * Loads a controller plugin. Resolves $name to Lynn_Controller_Plugin_$name.
- *
- * WARNING: This is a beta api, that means it's prone to change.
- *
- * @param string $name The name of the controller plugin to load.
- */
- public function loadPlugin($name) {
- $klass = 'Lynn_Controller_Plugin_' . $name;
- lynn_load($klass);
-
- $this->$name = new $klass(&$this);
- }
- }