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




$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);
// ...

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

return $app;



namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $middlewareGroups = [
        'web' => [
            \Illuminatech\UrlTrailingSlash\Middleware\RedirectTrailingSlash::class, // enable automatic redirection on incorrect URL trailing slashes
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // ...
        ],
    
        'api' => [
            // 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
            'throttle:60,1',
            // ...
        ],
    ];
    // ...
}



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;
}