Skip to main content

Route

Routing

Route

An understandable API for the WordPress Rewrite engine

Introduction

A route is a unique URL with its own callback.

The purpose of this package

Creating custom routes with the WordPress' rewrite API is not easy. There are a lot of steps and odd terms to learn to get started.

With this package it is made much easier. We have implemented the Laravel Routing, but use the WordPress Rewrite API underneath.

We support groups, prefixing and middleware the same way as Laravel does.

warning

Some helpers are not yet supported : (

Getting started

Routes are created in *.php files. Per default these are located in the /routes folder.

These files are automatically loaded when the application boots.

Caching

You can cache all located routes using wp artisan route:cache or as part of the wp artisan optimize command which is recommended in production. The cache file is /bootstrap/cache/route.php and can be deleted again using wp artisan route:clear

Creating routes

A route is created by using the route facade Illuminate\Support\Facades\Route.

Request types

Routes are created by using any of the following methods on the Route facade: get, post, put, patch, delete, options, any and match.

Each of these methods correspond to a given HTTP request type and the created route will only respond to request of the correct type.

This means that you can have multiple routes on the same url as long as they use different request types.

The any method responds to any valid request type and match takes as argument a list of methods to accept eq: Route::match('PUT','PATCH').

Grouping

You can group routes together using the Route::group method. All routes registered in the same group will get the same middlewares and prefixes applied.

routes/my-routes.php
<?php

use Illuminate\Support\Facades\Route;

// This is a very simple group that does nothing.
Route::group(function(){
Route::get('foo', function(){
echo "Hello foo route!";
});

Route::get('bar', function(){
echo "Hello bar route!";
});
});

Prefixes

A prefix is a string that is prepended to all routes in a given group.

This is useful for routes that share the same path.

routes/my-routes.php
<?php

use Illuminate\Support\Facades\Route;

Route::prefix('prefix')->group(function(){
// This route will be accessible at /prefix/foo
Route::get('foo', function(){
echo "Hello prefix/foo route!";
});

// This route will be accessible at /prefix/bar
Route::get('bar', function(){
echo "Hello prefix/bar route!";
});
});
note

Prefixes and middleware are applied to groups. So make sure to add a group to them!

Middleware

Middleware are functions that are called for a route before its callback or controller is called. Here you can validate, deny or format the request.

This is very useful for making sure a user has access to all routes in a group.

routes/my-routes.php
<?php

use Illuminate\Support\Facades\Route;

// The auth middleware is built in and checks if a user is logged in
// In this example that means that "Hello" and "bar" is only output for those that are logged in.
// Others would be redirected to the login screen
Route::middleware('auth')->group(function(){
Route::get('foo', function(){
echo "Hello logged in foo route!";
});

Route::get('bar', function(){
echo "Hello logged in bar route!";
});
});

Auth

The auth middleware authenticates a user.

It checks if a user is logged in and, optionally, whether the user has a given capability.

If the check does not pass then the middleware will redirect the user to the login screen and back again.

routes/my-routes.php
<?php

use Illuminate\Support\Facades\Route;

Route::middleware('auth')->group(function(){
Route::get('foo', function(){
echo "Hello logged in user!";
});
});

// Here the user must have the "administrator" capability
Route::middleware('auth:administrator')->group(function(){
Route::get('foo', function(){
echo "Hello administrator!";
});
});