PHP code example of svanthuijl / laravel-routable-models

1. Go to this page and download the library: Download svanthuijl/laravel-routable-models 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/ */

    

svanthuijl / laravel-routable-models example snippets


    'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */

        /*
         * Added last to not be overwritten by application routes...
         */
        Svanthuijl\Routable\RoutableServiceProvider::class,
    ])->toArray(),



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Svanthuijl\Routable\Interfaces\HasRoutes;
use Svanthuijl\Routable\Traits\InteractsWithRoutes;

class Example extends Model implements HasRoutes
{
    use InteractsWithRoutes;
//...

    /**
     * Defining the routes for this model
     * @return void
     */
    public function registerRoutes(): void
    {
        $this->addRoute()
            ->action('exampleAction')
            ->controller(ExampleController::class);
        $this->addRoute('post')
            ->action('exampleAction')
            ->controller(ExampleController::class)
            ->method('post');
    }

$model = new Example();
$model->slug = 'example-001';
$model->save();

$model->route; // Will return (string) "/example-001"
$model->getRoute('post'); // Will return (string) "/example-001/post"

class ExampleController extends BaseController
{
    public function exampleAction($model): string
    {
        return view('test-view', compact('model'));
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use Svanthuijl\Routable\Interfaces\HasRoutes;
use Svanthuijl\Routable\Traits\InteractsWithRoutes;

class TranslatableExample extends Model implements HasRoutes
{
    use HasTranslations;
    use InteractsWithRoutes;

    /**
     * The attributes that are translatable
     *
     * @var string[]
     */
    public array $translatable = [
        'slug'
    ];

    /**
     * Defining the routes for this model
     * @return void
     */
    public function registerRoutes(): void
    {
        $this->addRoute()
            ->action('exampleAction')
            ->controller(ExampleController::class)
            ->isTranslatable();
    }
//...

$model = new TranslatableExample();
$model->slug = [
    'en' => 'slug-in-english',
    'nl' => 'slug-in-dutch',
];
$model->save();

app()->setLocale('en')
$model->route; // Will return (string) "/en/slug-in-english"

app()->setLocale('nl')
$model->route; // Will return (string) "/nl/slug-in-dutch"

$model->getRoute('default', 'en'); // Will return (string) "/en/slug-in-english"
$model->getRoute('default', 'nl'); // Will return (string) "/en/slug-in-dutch"

class ExampleController extends BaseController
{
    public function exampleAction($model): string
    {
        app()->locale(); // Will always return the locale for the used route
        return view('test-view', compact('model'));
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Svanthuijl\Routable\Interfaces\HasRoutes;
use Svanthuijl\Routable\Traits\InteractsWithRoutes;

class LocalizedExample extends Model implements HasRoutes
{
    use InteractsWithRoutes;

    /**
     * Defining the routes for this model
     * @return void
     */
    public function registerRoutes(): void
    {
        $this->addRoute()
            ->action('exampleAction')
            ->controller(ExampleController::class)
            ->isLocalized();
    }
//...

$model = new LocalizedExample();
$model->locale = 'en';
$model->slug = 'slug-in-english';
$model->save();

$model->route; // Will return (string) "/en/slug-in-english" 

$model = new LocalizedExample();
$model->locale = 'nl';
$model->slug = 'slug-in-dutch';
$model->save();

$model->route; // Will return (string) "/nl/slug-in-dutch" 

    public function registerRoutes(): void
    {
        $this->addRoute('name', Svanthuijl\Routable\DefaultRouteGenerator::class)
            ->action('exampleAction')
            ->controller(ExampleController::class)
            ->fromProperty('slug) // The property to use to generate the route path
            ->isLocalized() // For localized models (cannot be used with isTranslatable)
            ->isTranslatable() // For translatable models (cannot be used with isLocalized)
            ->method('get') // Sets the method the route should listen to
            ->prefix('example') // Prepends the path with "example/"
            ->suffix('example') // Adds "/example" to the path
   }
shell 
php artisan vendor:publish --tag="routable-migrations"
shell
php artisan vendor:publish --tag="routable-config"
$this->addRoute()