diff --git a/SimplePagesPlugin.php b/SimplePagesPlugin.php index 4eb8540..ebed937 100644 --- a/SimplePagesPlugin.php +++ b/SimplePagesPlugin.php @@ -195,24 +195,26 @@ public function hookDefineRoutes($args) return; } - $router = $args['router']; - - // Add custom routes based on the page slug. - $pages = get_db()->getTable('SimplePagesPage')->findAll(); - foreach ($pages as $page) { - $router->addRoute( - 'simple_pages_show_page_' . $page->id, - new Zend_Controller_Router_Route( - $page->slug, - array( - 'module' => 'simple-pages', - 'controller' => 'page', - 'action' => 'show', - 'id' => $page->id - ) - ) - ); + // Add a custom route based on the page slugs. + $slugs = get_db()->getTable('SimplePagesPage')->findSlugs(); + if (empty($slugs)) { + return; } + + $router = $args['router']; + $quotedSlugs = array_map('preg_quote', $slugs); + $router->addRoute('simple_pages_show_pages', new Zend_Controller_Router_Route_Regex( + '(' . implode('|', $quotedSlugs) . ')', + array( + 'module' => 'simple-pages', + 'controller' => 'page', + 'action' => 'show', + ), + array( + 1 => 'slug', + ), + '%s' + )); } /** diff --git a/controllers/PageController.php b/controllers/PageController.php index 9de33d3..e562e50 100644 --- a/controllers/PageController.php +++ b/controllers/PageController.php @@ -13,12 +13,15 @@ */ class SimplePages_PageController extends Omeka_Controller_AbstractActionController { + /** + * Show a simple page. + */ public function showAction() { - // Get the page object from the passed ID. - $pageId = $this->_getParam('id'); - $page = $this->_helper->db->getTable('SimplePagesPage')->find($pageId); - + // Get the page object from the passed slug. + $slug = $this->_getParam('slug'); + $page = $this->_helper->db->getTable('SimplePagesPage')->findBySlug($slug); + // Restrict access to the page when it is not published. if (!$page->is_published && !$this->_helper->acl->isAllowed('show-unpublished')) { diff --git a/models/SimplePagesPageTable.php b/models/SimplePagesPageTable.php index 594eaac..a4c0879 100644 --- a/models/SimplePagesPageTable.php +++ b/models/SimplePagesPageTable.php @@ -23,7 +23,21 @@ public function findAllPagesOrderBySlug() $select = $this->getSelect()->order('slug'); return $this->fetchObjects($select); } - + + /** + * Find all slugs. + * + * @return array All slugs of pages. + */ + public function findSlugs() + { + $alias = $this->getTableAlias(); + $select = $this->getSelect(); + $select->reset(Zend_Db_Select::COLUMNS); + $select->from(array(), array($alias . '.slug')); + return $this->fetchCol($select); + } + public function applySearchFilters($select, $params) { $alias = $this->getTableAlias(); @@ -52,9 +66,24 @@ public function applySearchFilters($select, $params) $select->order("{$alias}.title ASC"); break; } - } + } } - + + /** + * Retrieve a simple page by slug. + * + * @param sllug $slug Slug of the page to retrieve. + * @return SimplePagePage|null The simple page that is returned. + */ + public function findBySlug($slug) + { + $select = $this->getSelect(); + $select->where($this->getTableAlias() . '.slug = ?', $slug); + $select->limit(1); + $select->reset(Zend_Db_Select::ORDER); + return $this->fetchObject($select, array()); + } + /** * Retrieve child pages from list of pages matching page ID. *