dev-master
9999999-dev http://github.com/josegonzalez/cakephp-dynamic-routeAutomagicallydatabase-backed dynamic routes for cakephp
MIT
The Requires
database cakephp routes
Automagicallydatabase-backed dynamic routes for cakephp
Read routes from a database into routes.php
quickly and easily, (*2)
As with the other route class I built, someone asked in IRC if it would be possible to read the routes from the database and load them into the app/Config/routes.php
file on the fly. I decided this was true, and this is the result., (*3)
[Using Composer], (*4)
Add the plugin to your project's composer.json
- something like this:, (*5)
{ "require": { "josegonzalez/cakephp-dynamic-route": "1.0.0" } }
Because this plugin has the type cakephp-plugin
set in it's own composer.json
, composer knows to install it inside your /Plugins
directory, rather than in the usual vendors file. It is recommended that you add /Plugins/DynamicRoute
to your .gitignore file. (Why? read this.), (*6)
[Manual], (*7)
app/Plugin
DynamicRoute
[GIT Submodule], (*8)
In your app directory type:, (*9)
git submodule add git://github.com/josegonzalez/cakephp-dynamic-route.git Plugin/DynamicRoute git submodule init git submodule update
[GIT Clone], (*10)
In your plugin directory type, (*11)
git clone git://github.com/josegonzalez/cakephp-dynamic-route.git DynamicRoute
In 2.0 you need to enable the plugin your app/Config/bootstrap.php
file:, (*12)
CakePlugin::load('DynamicRoute');
If you are already using CakePlugin::loadAll();
, then this is not necessary., (*13)
Way near the bottom of your app/Config/routes.php
file, before the line where the default routes are loaded, add the following:, (*14)
App::uses('FancyRoute', 'DynamicRoute.Lib'); FancyRoute::connectFancyRoutes();
You can now remove all other hacks from your app/Config/routes.php
file., (*15)
What we've enabled is creating spec|slug
routes in the database. A spec
would be the internal CakePHP mapping, like posts/view?id=45
or events/calendar?date=2011-11-01&category=lol
, while the corresponding slugs
might be something like /why-isnt-this-pup-asleep
or /manchester/cakephp-developers-dance-to-beyonce
., (*16)
This spec|slug
system allows one to have a specialized table for routing - by default the dynamic_routes
table - which can be used across multiple models and controllers if necessary. It allows a developer to create a simple interface for building internal application routes that a non-developer can use at a later date. This is extremely useful when building content management systems that need a Joomla or Wordpress-like routing system., (*17)
FancyRoute::connectFancyRoutes()
takes an optional array for configuration the route loading:, (*18)
model
: String or Object referencing a CakePHP Model to use for loading records.
DynamicRoute.DynamicRoute
load
custom find method that returns specs mapping to slugscacheKey
: Key used for caching the dynamic routes to disk
dynamic_routes
cache
: Whether to cache the db queries
Because of the way in which this class works, it is not necessary to call Router::connect()
on any of the dynamic routes, as this is called internally by the FancyRoute
class., (*19)
The DynamicRoute
model contains methods for turning a given specification into an internal cakephp request, whether that be a string or array., (*20)
You will currently have to create valid spec|slug
records yourself. Slugs should be prepended with a /
character, but should not be followed by one. Specs are regular web requests, $_GET
style., (*21)
There is a helper method on the DynamicRoute
model called saveNew()
; This method takes a spec
and slug
, or an array of data, and returns whether or not the save is successful. It will also do it's best to ensure that the data is properly setup by normalizing specifications and ensuring there slug is in the correct format., (*22)
Copyright (c) 2011 Jose Diaz-Gonzalez, (*23)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*24)
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*25)
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*26)
Automagicallydatabase-backed dynamic routes for cakephp
MIT
database cakephp routes