Download the PHP package webflorist/routetree without Composer

On this page you can find all versions of the php package webflorist/routetree. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package routetree

RouteTree: Advanced Route Management for Laravel (>=v5.5)

Build Status Latest Stable Version

This package includes a special API for creating and accessing Laravel-routes and route-related information. It's main concept is to create a hierarchical multi-language RouteTree using an expressive syntax (mostly mimicking Laravel's own). Using that hierarchy, RouteTree can be used to easily create:

Here is a complete feature overview:

Table of Contents

Installation

  1. Require the package via composer:
    php composer require webflorist/routetree
  2. Publish config:
    php artisan vendor:publish --provider="Webflorist\RouteTree\RouteTreeServiceProvider"
  3. Define all locales you want to use on your website under the key locales inside your routetree.php config file.
    E.g.: 'locales' => ['en','de'].
    (Alternatively you can set it to null to enforce a single-language app (using config app.locale).)

Note that this package is configured for automatic discovery for Laravel. Thus the package's Service Provider Webflorist\RouteTree\RouteTreeServiceProvider as well as the RouteTree alias will be automatically registered with Laravel.

Accessing the RouteTree-service

There are several ways to access the RouteTree service:

The following code examples will use the facade RouteTree::.

Defining the RouteTree

Just like with Laravel's own routing, your can define the RouteTree in your routes/web.php.

For better comparability of syntaxes, the following examples will correspond to the ones presented in Laravel's Routing documentation where possible. They will also assume 2 configured languages ('en','de') - if not otherwise stated.

Basic Routing

The node() method creates a RouteNode with name (and ID) foo and is then setup using the closure in it's second parameter.

A RouteNode itself is comparable to Laravel's Route Groups. It does not per se result in any registered routes, but centralizes and shares various data (e.g. path, middleware, namespace, etc.) with it's actions and inherits them to any child nodes.

The $node->get() call adds a RouteAction named get using the HTTP request method GET and a closure as it's callback.

A RouteAction results in one generated Laravel Route per configured language.

The above code will register the following routes:

In the above example, the RouteNode's setup closure is skipped and instead the get call is directly chained to the node call. This is an alternative way to setup (which returns a RouteNode and thus allows chaining of various fluent methods), resulting in a more readable one-liner. Once a RouteNode becomes a little more complex and has several child-nodes, using a setup-closure is recommended instead. Also be wary, that action-creating methods (such as get, post, redirect, view, etc.) return the RouteAction object instead of the RouteNode.

Both syntax variants will be used in the following examples.

Available RouteActions

RouteNodes provide public methods to register RouteActions that respond to any HTTP verb:

Redirect Actions

You can also define redirecting nodes and state the target nodes by their ID:

By default, $node->redirect() returns a 302 status code.

You can customize the status code using the optional second parameter:
$node->redirect('there', 301);.

You can also use $node->permanentRedirect() to return a 301 status code.

View Actions

If a RouteNode should only return a view, you can use the view method:

You can pass data to the view via the second parameter of the view method.

Configuring the Root Node

The RouteTree::node() method used in the above examples automatically creates nodes with the root node as parent. You can configure the root node itself using the root method instead:

You cannot state a name for the root node. It's name and ID will always be an empty string ('').

Adding Child Nodes

There are several ways to create a node as a child of another node:

The first variant will be used in any further examples. It builds the RouteTree using nested closures, which has the benefit of representing the hierarchical RouteTree within the defining code indentation.

Child nodes will automatically receive a unique node ID representing the hierarchy of it's ancestors. For example a child with name bar of a parent called foo will have the ID foo.bar.

The same happens with path segments, resulting in for example en/foo/bar.

You can disable inheriting the segment to it's descendants by calling $node->inheritSegment(false). This is useful, if you want to use a RouteNode simply for grouping purposes without a representation in the URL-path.

As with Laravel's Route groups, middleware and (controller-)namespaces will be also inherited by default.

Path Segments

By default a RouteNode's name will also be it's path segment. But you can also state a different segment for a node by calling the RouteNode's segment method:

The above code will register the following routes:

You can define localized path segments by handing a LanguageMapping object including segments for all languages:

This will register the following routes:

You can also handle segment-translations via your language-files using the Automatic Translation functionality.

Middleware

You can assign middleware to RouteNodes using:

This will attach the auth middleware to all of the RouteNode's actions and inherit it to all descendant nodes.

Middleware-parameters can be stated in the second parameter of the middleware method.

Inheritance of middleware can be disabled by handing boolean false as the third parameter of the middleware method.

In case you want a descendant node to NOT use an inherited middleware, simply state the following in the descendant's callback:

There might also be situations, where you want a specific action of a RouteNode to have additional middleware or skip a middleware defined on the RouteNode. You can achieve this by chaining the middleware call to the action-call. Here is an example:

This will register the following routes:

Controller Namespaces

By default all Controller@method callback definitions will use App\Http\Controllers as the namespace.

Using a RouteNode's namespace method will append a segment to that namespace and inherit it to it's descendants. Inheritance can be overruled by simply prefixing the namespace with a backslash.

Route Parameters

The following code will result in the creation of the routes en/user/{id} and de/user/{id}.

You can also set regular expression constraints for parameters:

When using parameter or resource nodes, you might also want to be able to translate a route key (e.g. to realize a language-switching menu for a blog-article, which has different slugs for different languages.)

There are two ways to achieve this:

Resourceful RouteNodes

Akin to Laravel's Route::resource() method, RouteTree can also register resourceful routes:

This will generate a full set of resourceful routes in all languages:

HTTP-Verb Route Name URI Action / Controller Method
GET en.photos.index /en/photos index
GET en.photos.create /en/photos/create create
POST en.photos.store /en/photos store
GET en.photos.show /en/photos/{photo} show
GET en.photos.edit /en/photos/{photo}/edit edit
PUT/PATCH en.photos.update /en/photos/{photo} update
DELETE en.photos.destroy /en/photos/{photo} destroy
GET de.photos.index /de/photos index
GET de.photos.create /de/photos/create create
POST de.photos.store /de/photos store
GET de.photos.show /de/photos/{photo} show
GET de.photos.edit /de/photos/{photo}/edit edit
PUT/PATCH de.photos.update /de/photos/{photo} update
DELETE de.photos.destroy /de/photos/{photo} destroy

Partial resource routes are also supported using the only or except methods:

Resource nodes can also have child-nodes. In this case call the child method on $node->resource instead of $node:

The above code will additionally generate the following routes:

Retrieving Nodes from the RouteTree

Now that we have defined the RouteTree, it's RouteNodes can be accessed anywhere in your application using the route_node() helper:

If RouteTree fails to find the current/specified node, it will throw a NodeNotFoundException, except a fallback node is set in the config routetree.fallback_node. The default config sets the fallback node to the root node, since you will probably want to inhibit NodeNotFoundExceptions in a production environment.

Generating URLs

One of RouteTree's central use cases is to create language-agnostic links. Both RouteNodes and RouteActions have a getUrl() method, that returns a RouteUrlBuilder object, which will generate the corresponding URL when cast to a string.

(string) route_node('company.team.contact')->getUrl() will return the URL of the RouteNode's action. If a node has multiple actions, it will return the link to it's first get action (or index actions with resources).

The returned RouteUrlBuilder object has several fluent setters to modify the generated link:

Route Payload

You can define an access any information you want to a RouteNode using it's associated RoutePayload object, which is publicly accessible via a node's payload property.

Defining Payload

You can set a payload item directly in the RoutePayload object using the following syntax-options:

The value of a payload can be any data type as well as a Closure. The Closure will receive two parameters:

As with path segments, any payload-item can also be multilingual using a LanguageMapping object:

You can also handle payload translations via your language-files using the Automatic Translation functionality.

If you want to have different values of a payload depending on an action, you can override a RouteNode's payload using a RouteAction's payload. Here is an example:

For parameter/resource nodes there is also the possibility to fetch payload from an Eloquent model. There are two requirements for this:

  1. An Eloquent model must be stated using the model method of a RouteParameter or RouteResource:
    $node->resource('photos', 'PhotoController')->model('App\Photo'); or
    $node->parameter('photo')->model('App\Photo');
  2. The model must implement the interface Webflorist\RouteTree\Interfaces\ProvidesRoutePayload and subsequently the getRoutePayload method. Here is an example implementation:

Retrieving Payload

Payload can be retrieved anywhere in your app. using the get method of a RoutePayload. Example using the current RouteNode and RouteAction:

This will look for the title payload using the following order:

  1. Payload set directly in this class.
  2. Payload set in the RouteNode's RoutePayload (only if this RoutePayload is RouteAction-specific.)
  3. Payload returned from an Eloquent Model (only if RouteNode has a RouteParameter associated with an Eloquent Model, that implements ProvidesRoutePayload)
  4. Using Auto-Translation by searching for payload at a translation-key relative to the RouteNode's ID (see Automatic Translation).

There are multiple use-cases, where this payload functionality can be useful This is very useful to e.g.:

Special title and navTitle payloads

Page titles (for meta tags, canonical tags, title attributes of links, navigation menus, breadcrumbs, h1-tags, etc.) are probably one of the most used applications for payloads. Also quite often you might want to have a special (shorter) title for pages in navigation menus. To simplify handing of this, RouteNodes and RouteActions have special getTitle() and getNavTitle() methods, that add some additional fallback magic:

To utilize this magic, always use e.g. route_node()->getTitle() instead of route_node()->payload->get('title') and route_node()->getNavTitle() instead of route_node()->payload->get('navTitle').

Automatic Translation

RouteTree also includes some magic regarding automatic translation. The basic concept is to map the hierarchy of the RouteTree to a folder-structure within the localization-folder.

The config-key localization.base_folder sets the base-folder for the localization-files- and folders utilized by RouteTree. The default value is pages, which translates to the folder \resources\lang\%locale%\pages.

There are 2 seperate auto-translation-functionalities:

  1. Auto-translation of a node's path-segment and payload (e.g. title, navTitle, description, etc.)
  2. Auto-translation of regular page-content.

Auto-translation of a node's path-segment and payload

This provides an easy and intuitive way of configuring multi-language variants of the path-segment, page-title, or any other custom information for routes through Laravel's localization-files.

Each RouteNode is represented as a folder, and within the folder for a node resides a file, that contains all auto-translation-information. How this file is named is configured under the config-key localization.file_name. The default value is pages, which means information on all 1st-level-pages should be placed in this file: \resources\lang\%locale%\pages\pages.php

Example: Let's assume, you have defined the following RouteNodes (any actions or other options are missing for simplicity's sake):

Please note, that no path-segment, page-titles or custom information is defined on any node. We will use auto-translation for this.

To use auto-translation, the following file- and folder-structure should be present within the defined base-folder for each locale (per default \resources\lang\%locale%\pages):

Each pages.php-file includes auto-translation information for the child-nodes of the node, which corresponds to the folder it resides in. Let's see a german-language example of the contents of these files:

./pages.php:

Note that there is an additional entry in the title-array with an empty string as the key and "Home" as the value. This is title of the root-node (as the root-node's ID and name is always an empty string '').

./company/pages.php:

./company/team/pages.php:

With this setup, the segments defined in the language-files will automatically be used for the route-paths of their corresponding nodes.

Also the title will be fetched with each getTitle-call submitted for a specific node (e.g. route_node('company.team.service')->getTitle() would return Büro, if the current locale is de.

The same thing is possible with the description (or any other payload). (e.g. route_node('company.team.service')->payload->get('description') would return Hier finden Sie unsere Service-Mitarbeiter., if the current locale is de.

You can also set action-specific titles or navTitles via auto-translation by appending an underscore and the action to the node-name. This is very useful for resource nodes. Here is an example:

Auto-translation of regular page-content

With most websites you will want to translate page-content in your views. RouteTree includes a handy helper-function called trans_by_route(), that will use the same folder-structure but with the language-file named as the last RouteNode.

Using the example above the location of this file for the office page would be : ./company/team/office.php

Caching

If you are using Laravels route caching, RouteTree must cache it's own data too. So instead of 'artisan route:cache' use RouteTree's caching-command, which will also take care of caching Laravel's routes:

XML Sitemap

Having an up-to-date sitemap.xml file is an important criterion for a modern search engine optimized website. RouteTree includes functionality to create such a file.

The following an artisan command will create a static XML sitemap file:

Per default the output-file will be at 'public/sitemap.xml'. You can however configure this in RouteTree's config file.

You can also enable a route to provide the sitemap dynamically (see config-options under routetree.sitemap.route).

Any URL's in the sitemap will use config('app.url') as the base url automatically. But you can also state a different value unfer the routetree.sitemap.base_url config.

Per default all routes created with RouteTree will be present in the sitemap. There are some exclusion criteria though:

Furthermore you can also explicitly exclude a RouteNode (and all it's children) from the sitemap:

A sitemap.xml also allows the definition of additional information for search engines (see https://www.sitemaps.org/protocol.html#xmlTagDefinitions). You can state this data for a node using the following code:

Furthermore you can also use payload translation (either via an Eloquent model or via language files) to automatically retrieve these values.

API (requires at least Laravel 5.6!)

RouteTree also includes an API, that allows fetching information about routes registered with RouteTree. The API must be enabled via config routetree.api.enabled and has the default base-url api/routetree/ (also configurable).

At the moment there are 2 endpoints:

Events

RouteTree dispatches events in various cases:

Important RouteTree-methods

For the already mentioned and explained methods root(), node() please see the corresponding sections above.

Here are some other useful methods of the RouteTree-class:

Important RouteNode-methods

For the already mentioned and explained methods getUrl, getTitle and getNavTitle please see the corresponding sections above.

Here are some other useful methods of the RouteNode-class:

Helper functions

Several helper-functions are included with this package:


All versions of routetree with dependencies

PHP Build Version
Package Version
Requires laravel/framework Version >=5.5
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package webflorist/routetree contains the following files

Loading the files please wait ....