Git Product home page Git Product logo

routetreebundle's Introduction

BecklynRouteTreeBundle

Adds a simple implementation for an automatic generation of a route tree to build a menu.

Installation

Install the bundle via packagist:

    // ...
    require: {
        // ...
        "becklyn/route-tree-bundle": "^2.0"
    },
    // ...

Load the bundle in your app/AppKernel.php:

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new \Becklyn\RouteTreeBundle\BecklynRouteTreeBundle(),
        );
        // ...
    }

Defining The Route Tree Nodes

You add elements to the tree by setting the options of the routes:

homepage:
    path: /

my_route:
    path: /my_route
    options:
        tree:
            parent: homepage
            title:  "My Route"

Path Options

route:
    # ...
    options:
        tree:
            parent:     homepage       # the name of the parent node
            priority:   0              # (optional) the 
            title:      "abc"          # (optional) title of the node
            parameters: {}             # (optional) the default values for the parameters
            security:   ~              # (optional) the security expression

parent must be set. Also all referenced parent-routes need to exist.

Priority

All child nodes of all nodes are sorted by descending priority.

Hidden

All items are automatically hidden, if they have no title set (or if the security expression evaluates to false).

Parameters

The parameters can define default values for parameters:

page_listing:
    path: /listing/{page}
    options:
        tree:
            parameters:
                page: 1

If you do not define a value, the parameter is looked up in the request attributes of the current request. If it doesn't find anything there, 1 is used.

Security

Every node can have a custom security expression, that is evaluated when printing the tree. All nodes where the security expression evaluates to false are automatically hidden (including all their child nodes).

If no explicit security expression is given, the builder tries to infer the expression from the linked controller by combining @IsGranted() and @Security() annotations from the action method and the controller class. If @IsGranted() is used with the subject attribute, nothing is infered.

Extra Parameters

You can define additional parameters, that can be used in the menu template. All path options that are not recognized (see "Path Options") are automatically added as extra parameters.

route:
    options:
        tree:
            parent: homepage
            title: Pages
            icon: pages

These extra parameters are available in the template under the extra property:

{{ item.extra("icon") }}

Error Cases

If the page tree is invalid a InvalidRouteTreeException is thrown, on the first construction of the page tree.

Short Syntax

In simple cases, the config can be simplified to:

route:   
    options:
        tree: "parent"
        
# ... means the same as ...

route:   
    options:
        tree: 
            parent: "parent"

Rendering the Route Tree

There is an automatic menu builder, that you can just use in the templates:

{{- route_tree_render("my_route", {...}) -}}

The first parameter is the parent node of which the menu should be used. The second (optional) argument are the KnpMenu render options.

Getting The Route Tree

You can inject the service Becklyn\RouteTreeBundle\Tree\RouteTree and use it to retrieve a node:

    // get a node with a specific route. With this node you can traverse the route tree.
    $treeUnderMyRoute = $routeTree->getNode("my_route");

The return value is a Becklyn\RouteTreeBundle\Node\Node.

routetreebundle's People

Contributors

apfelbox avatar gemineye avatar keichinger avatar lukasbrodbeck avatar tkasper avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

routetreebundle's Issues

Is using route parameters really a good idea?

While it is convenient, there are quire a few false positives, where a parameter that was null will be set to a value inside the controller, but the route parameter is still null.

Maybe adding a RouteTreeContext could work, which is initialized with the route parameters, but can be overwritten in the app.

Add caching support

The whole tree creation should be cached and only produced on demand (as in "when warming the cache" or when running in $debug mode).

'null' as default is not possible for optional parameter.

The generated navigation link is '/route/1' and not '/route'

some_route:
    path: /route/{parameter}
    defaults:
        _controller: MyController
        parameter: ~
    requirements:
        parameter: \d+
    options:
        page_tree:
            parent: home
            parameters: {'parameter': ~}
            title: Title

Ability to manipulate positioning

For the RouteTreeBundle I'd love to have the ability to manipulate the positioning of a route inside the tree without having to re-position the route definitions inside the routing.yml.

Imagine a feature that works similarly to Egeloen's OrderedFormBundle with an option to set a route before or after another.

Retrieve `security` information from the corresponding controller/action

In the past we've already discussed this:
To minimize code duplication it'd be super nice if we could retrieve security information from a route's corresponding controller and/or action, depending on where the @Security annotation has been declared. Since right now one needs add a @Security annotation to a controller/action and then also duplicate the information over to the corresponding route in order to have the RouteTree/Menu adapt it.

Things that might need to be considered when implementing this feature:

  • Check whether honoring @Security through inheritance works as expected (aka. when a base class controller has @Security("is_granted('ROLE_ADMIN')") then any derived controller automatically inherits it.
  • Routes using the FrameworkBundle:Redirect:* controller and action need to be followed to their corresponding target (at least if it's internal) and then use the target controller's @Security information

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.