Giter VIP home page Giter VIP logo

fat-free-routes's Introduction

FatFreeRoutes

Note:

This tool has been completely refactored into a plugable architecture to allow for future expansion. Note the following:

  1. The command line parameter names have changed
  2. The @routeJS tag is no longer supported, and has been replaced by the [js] modifier.
  3. The documentation is a work-in-progress.

PHP >= 7.0 (To use the tool), PHP >= 5.4 (For generated code)

This development tool allows one to specify routes in the Fat Free Framework in DocBlock format in the controller class. Furthermore, it is build with a pluggable interface to allow for future expansion beyond route generation.

Example

class MyFatFreeController {

   /**
    * @route GET @alias: /path/here
    *
    * @param \Base $f3
    * @param array $args
    */
   public function someHandler(\Base $f3, $args) {
      //...
   }

}
 

Adding the @route tag above is equivalent to calling

$f3->route('GET @alias: /path/here', 'MyFatFreeController->someHandler');

or specifying the route in a config file.

The tool, f3routes produces a php file suitable to be required in index.php and provides a method to install the routes.

function installRoutes($includeDev = true) {
   // ...
}

Require the generated file in index.php and call installRoutes($includeDev); before calling $f3->run();

There is also the option to generate a JavaScript file so that routes can be easily built in Javascript for front-end use.

Tags

The following DocBlock tags can be used

@route Applies to a class method, and follows the same syntax as Fat Free Framework routes, using the syntax

   @route <METHOD> @alias: path [ajax|cli|sync]

Replaceable tokens can be used in the path. The alias is optional, as are the route modifiers in brackets [ ].

f3routes supports additional values in the route modifier portion surrounded by brackets [ ]. This can be a comma-separated list of the existing F3 modifiers (ajax, cli, or sync), and the following additional modifiers:

  • ttl=<number> Set the $ttl parameter to Base->route()
  • kbps=<number> Set the $kbps parameter to Base->route()
  • js Expose the alias in the Javascript code.

The order of the items in the modifier section in unimportant, except that only the last one of ajax, sync, or cli are preserved.

Here are some examples of @route tags using modifiers:

   @route GET|POST @alias: /some/path/@id [sync]
   @route GET @alias2: /some/other/path [ajax,js,ttl=3600]

@devroute Same as @route but only installed if installRoutes() is called with true.

@routeBase Specified in the DocBlock for the class, this prepends a path fragment to all of the route paths specified in the methods by @route or @devroute.

@routeMap and @devrouteMap The equivalent of the F3 map function, these create routes for RESTful controllers. These also allow specification of an alias for exposure in the Javascript output, which is enabled by adding [js] at the end of the tag:

/**
 *
 * @routeMap @mapAlias: /company/@id [js]
 *
 */
class Company {
    // ...
}

This is equivalent to

$f3->map('/company/@id', 'Company');

If a @routeBase is set, the map path will also be prepended.

Note that f3routes supports the proposed PSR-5 PHPDoc standard: all tags can be prefixed or namespaced with f3routes. The following are equivalent:

   /**
    *
    * @route GET /mypath
    * @f3routes-route GET /mypath
    * @f3routes\route GET /mypath
    * @\f3routes\route GET /mypath
    */
    public function myHandler(....) {
        // ...

Command Line Parameters

  • -f, --force

    Force the entire route map to be regenerated. If no cache file is specified, this has no effect, as the route table will be generated from scratch in that case.

  • -v, --verbose

    Verbose output.

  • --cache-file=<file>

    If specified, a cache-file will save the resulting route map, so that only files that have changed need to be re-parsed. The same cache file should be used on subsequent calls for the same project.

  • --source-dir=<directory>

    This is required. Specifies the directory that contains PHP files. This parameter can be specified more than once. Directories are scanned recursively.

  • --output-php=<file>

    This is required. Specifies the file to be written with the generated PHP code.

  • --output-js=<file>

    This is an optional parameter. If specified, a fle containing JS code and a route map will be produced. Only routes explicitly flagged for JS output will be emitted,

One use case is to incorporate f3routes into a file watcher, and update the routes in real-time as the controller classes are modified.

Example

   f3routes --source-dir=/myprojects/src/controllers \
         --output-php=/myproject/src/generated/routes.php \
         --cache-file=/myproject/src/generated/cache.f3r

Installation

f3routes can be installed via composer:

   composer require --dev richardgoldstein/fat-free-routes

You may need to specify the minimum stability as this is still in dev.

Once installed via composer, f3routes can be found in vendor/bin:

   ./vendor/bin/f3routes --controller-dir=...

or

   composer exec f3routes ...

I include it in a gulp task:

var run=require('gulp-run');

// ...

gulp.task('php-routes', ['clean-dist-app'], function(cb) {
    var cmd = new run.Command([
        './vendor/bin/f3routes',
        '--cache-file=./conf/route-cache.f3r',
        '--source-dir=./src/controllers',
        '--output-php=./conf/routes.php',
        '--output-js=./assets/js/generated/routes.js'
    ].join(' '));
   cmd.exec('', cb);
});

TODO

  1. Elaborate on the JavaScript output
  2. Plug-In documentation
  3. Needs new unit tests since refactoring.
  4. More examples
  5. How to use the rendered PHP and JavaScript

fat-free-routes's People

Contributors

richgoldmd avatar

Stargazers

Jakofff avatar evan70 avatar Frank avatar  avatar Njuguna Mureithi avatar Ayesh Karunaratne avatar  avatar Christian Knuth avatar

Watchers

 avatar  avatar

fat-free-routes's Issues

Support for Caching TIme and Bandwidth Throttling

Add support for $ttl and $kbps arguments in the DocBlock route.

I favor using an option in brackets like done for [js] for @RouteMap - adding options to the modifiers in the route and filtering them out before generating the php.

So where we might specify:

@route GET @alias: /path/to/route

we can use

@route GET @alias: /path/to/route [ttl=3600, kbps=64]

Or in the case where we use one of the F3 modified (ajax, cli, sync):

@route GET @alias: /path/to/route [ajax]

becomes

@route GET @alias: /path/to/route [ajax,ttl=3600]

If this parsing logic is added, we could add js also, and remove the need for @routeJS.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.