Example #1
0
 public static function handlers($handlers = null)
 {
     if (!static::$_handlers) {
         static::$_handlers = array('binding' => function ($request, $options) {
             $model = $options['binding'];
             $name = $options['name'];
             $model = Libraries::locate('models', $model ?: Inflector::pluralize($name));
             if (!$model || is_string($model) && !class_exists($model)) {
                 $msg = "Could not find resource-mapped model class for resource `{$name}`.";
                 throw new UnmappedResourceException($msg);
             }
             return $model;
         }, 'isRequired' => function ($request, $options, $result = null) {
             $name = $options['name'];
             $required = $options['required'];
             $model = $options['binding'];
             $isCountable = $result && $result instanceof Countable;
             $isValid = $result && !$isCountable || $isCountable && $result->valid();
             if ($isValid || !$required) {
                 return $result;
             }
             $model = is_object($model) ? get_class($model) : $model;
             $message = "Resource `{$name}` not found in model `{$model}`.";
             throw new ResourceNotFoundException($message);
         }, 'default' => array(function ($request, $options) {
             $isRequired = Resources::handlers('isRequired');
             $query = (array) $options['call'] + array('all');
             $call = $query[0];
             unset($query[0]);
             return $isRequired($request, $options, $options['binding']::$call($query));
         }, 'create' => function ($request, $options) {
             return $options['binding']::create($request->data);
         }));
     }
     if (is_array($handlers)) {
         static::$_handlers = $handlers + static::$_handlers;
     }
     if ($handlers && is_string($handlers)) {
         return isset(static::$_handlers[$handlers]) ? static::$_handlers[$handlers] : null;
     }
     return static::$_handlers;
 }
/**
 * Connect the rest of `PagesController`'s URLs. This will route URLs like `/pages/about` to
 * `PagesController`, rendering `/views/pages/about.html.php` as a static page.
 */
Router::connect('/pages/{:args}', 'Pages::view');
/**
 * Add the testing routes. These routes are only connected in non-production environments, and allow
 * browser-based access to the test suite for running unit and integration tests for the Lithium
 * core, as well as your own application and any other loaded plugins or frameworks. Browse to
 * [http://path/to/app/test](/test) to run tests.
 */
if (!Environment::is('production')) {
    Router::connect('/test/{:args}', array('controller' => 'lithium\\test\\Controller'));
    Router::connect('/test', array('controller' => 'lithium\\test\\Controller'));
}
Router::connect(Resources::export(array('Todos')));
/**
 * ### Database object routes
 *
 * The routes below are used primarily for accessing database objects, where `{:id}` corresponds to
 * the primary key of the database object, and can be accessed in the controller as
 * `$this->request->id`.
 *
 * If you're using a relational database, such as MySQL, SQLite or Postgres, where the primary key
 * is an integer, uncomment the routes below to enable URLs like `/posts/edit/1138`,
 * `/posts/view/1138.json`, etc.
 */
// Router::connect('/{:controller}/{:action}/{:id:\d+}.{:type}', array('id' => null));
// Router::connect('/{:controller}/{:action}/{:id:\d+}');
/**
 * If you're using a document-oriented database, such as CouchDB or MongoDB, or another type of
 /**
  * Tests that resource parameters with no matching model generate an exception.
  */
 public function testResourceQueryingWithInvalidModel()
 {
     $this->expectException('Could not find resource-mapped model class for resource `fzbszl`.');
     $result = Resources::get($this->_request('GET'), array('name' => 'fzbszl', 'call' => array('first') + compact('conditions')));
 }
use li3_resources\net\http\Resources;
/**
 * This filter intercepts the `run()` method of the `Dispatcher`, and first passes the `'request'`
 * parameter (an instance of the `Request` object) to the `Environment` class to detect which
 * environment the application is running in. Then, loads all application routes in all plugins,
 * loading the default application routes last.
 *
 * Change this code if plugin routes must be loaded in a specific order (i.e. not the same order as
 * the plugins are added in your bootstrap configuration), or if application routes must be loaded
 * first (in which case the default catch-all routes should be removed).
 *
 * If `Dispatcher::run()` is called multiple times in the course of a single request, change the
 * `include`s to `include_once`.
 *
 * @see lithium\action\Request
 * @see lithium\core\Environment
 * @see lithium\net\http\Router
 */
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    Environment::set($params['request']);
    foreach (array_reverse(Libraries::get()) as $name => $config) {
        if ($name === 'lithium') {
            continue;
        }
        $file = "{$config['path']}/config/routes.php";
        file_exists($file) ? include $file : null;
    }
    return $chain->next($self, $params, $chain);
});
Resources::bind("lithium\\action\\Dispatcher");