DooPHP IRC channel
// Ensure this line is commented out
//$config['AUTOROUTE'] = true;
// OR set to false
$config['AUTOROUTE'] = false;
// This should appear directly after the include of common.conf.php
include './protected/config/routes.conf.php';
// This should appear just before your Doo::app()->run() call
Doo::app()->route = $route;
<?php
include './protected/config/common.conf.php';
include './protected/config/routes.conf.php';
include $config['BASE_PATH'].'Doo.php';
include $config['BASE_PATH'].'app/DooConfig.php';
Doo::conf()->set($config);
// remove this if you wish to see the normal PHP error view
include $config['BASE_PATH'].'diagnostic/debug.php';
Doo::app()->route = $route;
Doo::app()->run();
$route['*']['/contact-us'] = array('MainController', 'contactUs');
$route['*']['/contact-us'] = array('ContactController', 'contactUs');
$route['*']['/contact-us'] = array('MainController', 'contactUs');
$route['*']['/contact-us'] = array('MainController', 'contactUs'); // Correct
// Above will match both http://example.com/contact-us AND
// http://example.com/contact-us/
$route['*']['/contact-us/'] = array('MainController', 'contactUs'); // Incorrect (trailing / in the route definition)
$route['*']['/help-and-support/privacy-policy'] = array('HelpAndSupportController', 'privacyPolicy');
$route['*']['/'] = array('MainController', 'home');
$config['ERROR_404_ROUTE'] = '/error';
$route['*']['/error'] = array('ErrorController', 'showError');
<?php
class ErrorController extends DooController {
public function showError() {
echo "An Unknown Error Has Occurred";
}
}
class MainController extends DooController {
public function test() {
echo "Test Function - Global Handler";
}
public function test_get() {
echo "Test Function - GET Handler";
}
public function test_post() {
echo "Test Function - POST Handler";
}
}
$route['*']['/test'] = array('MainController', 'test');
$route['get']['/test'] = array('MainController', 'test_get');
$route['post']['/test'] = array('MainController', 'test_post');
Test Function - GET Handler
Test Function - POST Handler
Test Function - Global Handler
$route['*']['/news/article/:id'] = array('NewsController', 'viewArticle');
$route['*']['/news/category/:category'] = array('NewsController', 'viewCategory');
<?php
class NewsController extends DooController {
public function viewArticle() {
// Fetch the article ID from the params array
$articleId = $this->params['id'];
// Code would go here to validate the ID and
// fetch the article from db etc...
echo "Viewing Article: {$articleId}";
}
public function viewCategory() {
// Fetch the category name from the params array
$categoryName = $this->params['category'];
// Code would go here to validate the category name and
// fetch the relevant articles from the db
echo "Viewing Category: {$categoryName}";
}
}
Viewing Article: 12 // When we call example.com/news/article/12/
Viewing Category: doophp // When we call example.com/news/category/doophp/
$route['*']['/news/article/:id/:name'] = array('NewsController', 'viewArticle');
$this->params['name']
$route['*']['/news/article/:id'] = array('NewsController', 'viewArticle');
$route['*']['/news/article/:id/:name'] = array('NewsController', 'viewArticle');
$route['*']['/news/:id'] = array('NewsController', 'viewArticle');
$route['*']['/news/:category'] = array('NewsController', 'viewCategory');
$route['*']['/:page'] = array('MainController', 'viewPage');
$route['*']['/news/article/:id/full-page'] = array('NewsController', 'viewArticleFullPage');
$route['*']['/news/article/:id/narrow'] = array('NewsController', 'viewArticleNarrow');
// Uncommenting the below line will cause it to catch both of the other urls
// $route['*']['/news/article/:id/:name'] = array('NewsController', 'viewArticle');
$route['*']['/news/article/:id/full-page'] = array('NewsController', 'viewArticleFullPage');
$route['*']['/news/article/:id/narrow'] = array('NewsController', 'viewArticleNarrow');
// You could however place it here like so to catch the other options
// $route['*']['/news/article/:id/:name'] = array('NewsController', 'viewArticle');
// OR you could also support 2 params inside static sections:
$route['*']['/news/article/:id/:name/full-page'] = array('NewsController', 'viewArticleFullPage');
$route['*']['/news/article/:id/:name/narrow'] = array('NewsController', 'viewArticleNarrow');
$route['*']['catchall']['/news/article/:all'] = array('NewsController', 'catchAllViewArticle');
// example.com/news/article/12
Array
(
[all] => 12
)
// example.com/news/article/12/some-article-name
Array
(
[all] => 12
[0] => some-article-name
)
// example.com/news/article/12/some/article/name
Array
(
[all] => 12
[0] => some
[1] => article
[2] => name
)
$route['*']['catchall']['/:all'] = array('MainController', 'catchEverything');
// This will not work!
// example.com/some-page
$route['*']['/:page'] = array('MainController', 'viewPage');
$route['*']['root']['/:page/wide-screen'] = array('MainController', 'viewPageInWideScreen');
$route['*']['/news/article/all.html'] = array('NewsController', 'viewAllArticlesAsHtml');
$route['*']['/news/article/all.xml'] = array('NewsController', 'viewAllArticlesAsXml');
$route['*']['/news/article/view/:id'] = array('NewsController', 'viewArticleAsHtml', 'extension' => '.html');
$route['*']['/news/article/view/:id'] = array('NewsController', 'viewArticleAsXml', 'extension' => '.xml');
$route['*']['/news/article/view/:id'] = array('NewsController', 'viewArticleAsHtml', 'extension' => array('.html', '.htm', '.php'));
<?php
class NewsController extends DooController {
public function viewArticleAsHtml() {
echo "View Article as HTML";
echo "Requested Extension: " . $this->extension; // Get the matching extension
}
}
Users browsing this forum: No registered users and 1 guest