PHP code example of illuminatech / url-trailing-slash

1. Go to this page and download the library: Download illuminatech/url-trailing-slash library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

illuminatech / url-trailing-slash example snippets




use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

$app = Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        // ...
    )
    ->withMiddleware(function (Middleware $middleware) {
        // ...
    })
    // ...
    ->create();

$app->register(new Illuminatech\UrlTrailingSlash\RoutingServiceProvider($app)); // register trailing slashes routing

return $app;



use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

$app = Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        // ...
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->prependToGroup('web', Illuminatech\UrlTrailingSlash\Middleware\RedirectTrailingSlash::class); // enable automatic redirection on incorrect URL trailing slashes
        // probably you do not need trailing slash redirection anywhere besides public web routes,
        // thus there is no reason for addition its middleware to other groups, like API
        // ...
    })
    // ...
    ->create();

$app->register(new Illuminatech\UrlTrailingSlash\RoutingServiceProvider($app)); // register trailing slashes routing

return $app;



namespace App\Http\Controllers;

use Illuminate\Support\Facades\Route;

Route::get('items/', ItemController::class.'@index')->name('items.index'); // enforce trailing slash
Route::get('items/{item}', ItemController::class.'@show')->name('items.show'); // enforce no trailing slash

// ...

echo route('items.index'); // outputs: 'http://example.com/items/'
echo route('items.show', [1]); // outputs: 'http://example.com/items/1'



namespace App\Http\Controllers;

use Illuminate\Support\Facades\Route;

Route::resource('items/', ItemController::class); // enforce trailing slash
Route::resource('categories', CategoryController::class); // enforce no trailing slash

// ...

echo route('items.index'); // outputs: 'http://example.com/items/'
echo route('items.show', [1]); // outputs: 'http://example.com/items/1/'

echo route('categories.index'); // outputs: 'http://example.com/categories'
echo route('categories.show', [1]); // outputs: 'http://example.com/categories/1'



namespace App\Http\Controllers;

use Illuminate\Support\Facades\Route;

Route::resource('items', ItemController::class, ['trailingSlashOnly' => 'index']); // trailing slash will be present only for 'index'
Route::resource('categories', CategoryController::class, ['trailingSlashExcept' => 'show']); // trailing slash will be present for all but 'show'

// ...

echo route('items.index'); // outputs: 'http://example.com/items/'
echo route('items.show', [1]); // outputs: 'http://example.com/items/1'

echo route('categories.index'); // outputs: 'http://example.com/categories/'
echo route('categories.show', [1]); // outputs: 'http://example.com/categories/1'



use App\Models\Item;
use Illuminate\Support\Facades\URL;

$items = Item::query()
    ->paginate()
    ->withPath(URL::current());



namespace Tests;

use Illuminate\Contracts\Console\Kernel;
use Illuminatech\UrlTrailingSlash\RoutingServiceProvider;

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = 



namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminatech\UrlTrailingSlash\Testing\AllowsUrlTrailingSlash;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use AllowsUrlTrailingSlash;
}