URL Generation

Introduction

Several helpers exist to assist you in generating URLs for your application. These are mainly helpful when building links in your templates and API responses, or when generating redirect responses to another part of your application.

The Basics

Generating Basic URLs

The url helper may be used to generate arbitrary URLs for your application. The generated URL will automatically use the scheme (HTTP or HTTPS) and host from the current request and/or configuration:

url('example/page');

Accessing The Current URL

If no path is provided to the url helper, a Anomaly\Streams\Platform\Routing\UrlGenerator instance is returned, allowing you to access information about the current URL:

// Get the current URL without the query string...
echo url()->current();

// Get the current URL including the query string...
echo url()->full();

// Get the full URL for the previous request...
echo url()->previous();

Each of these methods may also be accessed within view templates:

{{ url_current() }}

{{ url_full() }}

{{ url_previous() }}

URLs For Named Routes

The route helper may be used to generate URLs to named routes. Named routes allow you to generate URLs without being coupled to the actual URL defined on the route. Therefore, if the route's URL changes, no changes need to be made to your route function calls. For example, imagine your application contains a route defined like the following:

Route::get('/example/page', function () {
    //
})->name('page::example');

To generate a URL to this route, you may use the route helper like so:

echo route('page::example');

// http://example.com/example/page

You can also do this within your view templates:

{{ url_route('page::example') }}

Signed URLs

You can easily create "signed" URLs to named routes. These URLs have a "signature" hash appended to the query string which allows the application to verify that the URL has not been modified since it was created. Signed URLs are especially useful for routes that are publicly accessible yet need a layer of protection against URL manipulation.

For example, you might use signed URLs to implement a public "unsubscribe" link that is emailed to your customers. To create a signed URL to a named route, use the signedRoute method of the URL facade:

use Illuminate\Support\Facades\URL;

return URL::signedRoute('unsubscribe', ['user' => 1]);

If you would like to generate a temporary signed route URL that expires, you may use the temporarySignedRoute method:

use Illuminate\Support\Facades\URL;

return URL::temporarySignedRoute(
    'unsubscribe', now()->addMinutes(30), ['user' => 1]
);

Validating Signed Route Requests

To verify that an incoming request has a valid signature, you should call the hasValidSignature method on the incoming Request:

use Illuminate\Http\Request;

Route::get('/unsubscribe/{user}', function (Request $request) {
    if (! $request->hasValidSignature()) {
        abort(401);
    }

    // ...
})->name('unsubscribe');

Alternatively, you may assign the Illuminate\Routing\Middleware\ValidateSignature middleware to the route.

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
];

Once you have registered the middleware in your kernel, you may attach it to a route. If the incoming request does not have a valid signature, the middleware will automatically return a 403 error response:

Route::post('/unsubscribe/{user}', function (Request $request) {
    // ...
})->name('unsubscribe')->middleware('signed');

URLs For Controller Actions

The action function generates a URL for the given controller action. You do not need to pass the full namespace of the controller. Instead, pass the controller class name relative to the App\Http\Controllers namespace:

$url = action('[email protected]');

You may also reference actions with a "callable" array syntax:

use App\Http\Controllers\HomeController;

$url = action([HomeController::class, 'index']);

If the controller method accepts route parameters, you may pass them as the second argument to the function:

$url = action('[email protected]', ['id' => 1]);