GithubHelp home page GithubHelp logo

croute's Introduction

Croute

Build Status Coverage Status SensioLabsInsight

Convention based routing for PHP based on Symfony components.

Croute is great because:

  • You don't need to maintain a routing table
  • Promotes consistent code organization
  • Allows for customization through annotations and events

Install via Composer

Via the command line:

composer.phar require thewunder/croute ~1.0

Or add the following to the require section your composer.json:

"thewunder/croute": "~1.0"

Basics

Your index.php should look something like this:

$router = Router::create($eventDispatcher, ['Your\\Controller\\Namespace'], [$dependency1, $dependency2]);
$router->route($request);

Your controllers should look something like this:

namespace Your\Controller\Namespace

class IndexController extends Croute\Controller
{
    public function __construct($dependency1, $dependency2)
    {
        //...
    }

    /**
     * Will be available at http://yourdomain/
     * and require the "required" (body or querystring) request parameter
     */
    public function indexAction($required, $optional = null)
    {
        echo 'Crouter Controller'; //you can echo or return a symfony Response
    }

    /**
     * Available at http://yourdomain/test
     */
    public function testAction()
    {
        return new Response('Test Action');
    }
}

The name of the controller determines which url it appears as:

It supports nested namespaces so that:

Annotations

Croute optionally supports controller and action annotations through the excellent minime/annotations library. To add an annotation handler simply:

$router->addAnnotationHandler($myhandler);

Two annotations are included (but must be added) out of the box @httpMethod and @secure.

@httpMethod

Restricts the allowed http methods. Returns a 400 response if the method does not match.

    /**
     * @httpMethod POST
     */
    public function saveAction()

@secure

Requires a secure connection. If the connection is not https send a 301 redirect to the same url with the https protocol.

/**
 * @secure
 */
class IndexController extends Controller
{

Events

Symfony events are dispatched for every step in the routing process. A total of 12 events are dispatched in a successful request:

  1. router.request
  2. router.controller_loaded
  3. router.controller_loaded.{ControllerName}
  4. router.before_action
  5. router.before_action.{ControllerName}
  6. router.before_action.{ControllerName}.{actionName}
  7. router.after_action
  8. router.after_action.{ControllerName}
  9. router.after_action.{ControllerName}.{actionName}
  10. router.response_sent
  11. router.response_sent.{ControllerName}
  12. router.response_sent.{ControllerName}.{actionName}

The {ControllerName} will be sans 'Controller' and {actionName} sans 'Action' i.e IndexController::indexAction -> router.before_action.Index.index.

At any time before the response is sent, in an event listener you can set a response on the event to bypass the action and send instead.

    public function myListener(ControllerLoadedEvent $event)
    {
        $event->setResponse(new Response('PermissionDenied', 403));
    }

Error Handling

Proper error handling is not really something that I can do for you. It's up to you to determine how to do logging, how and when to render a pretty error page. To handle errors, implement the EventHandlerInterface and set your error handler on the router. Your class will be called when common routing events occur (i.e. 404 errors) and when there is an exception during the routing process.

Contributions

Yes please! This library is currently a one man show, and it works great for me. Please let me know if you have any ideas on improving croute.

croute's People

Contributors

thewunder avatar

Watchers

 avatar  avatar

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.