PHP code example of vlados / laravel-unique-urls

1. Go to this page and download the library: Download vlados/laravel-unique-urls 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/ */

    

vlados / laravel-unique-urls example snippets


return [
    // Locale => $language
    'languages' => [
        'bg_BG' => 'bg',
        'en_US' => 'en',
        'de_DE' => 'de',
    ],
    'redirect_http_code' => 301,
];

class MyModel extends Model
{
    use Vlados\LaravelUniqueUrls\HasUniqueUrls;

    public function urlStrategy($language,$locale): string
    {
        return Str::slug($this->getAttribute('name'),"-",$locale);
    }
    
    public function urlHandler(): array
    {
        return [
            // The controller used to handle the request
            'controller' => CategoryController::class,
            // The method
            'method' => 'view',
            // additional arguments sent to this method
            'arguments' => [],
        ];
    }

public function view(Request $request, $arguments = [])
{
    dd($arguments);
}

Route::get('{urlObj}', [\Vlados\LaravelUniqueUrls\LaravelUniqueUrlsController::class, 'handleRequest'])->where('urlObj', '.*');

public function isAutoGenerateUrls(): bool
{
    return false;
}

YourModel::all()->each(function (YourModel $model) {
    $model->generateUrl();
});

$model = new TestModel();
$model->disableGeneratingUrlsOnCreate();
$model->name = "Test";
$model->save();

public function urlHandler(): array
{
    return [
        // The Livewire controller
        'controller' => CategoryController::class,
        // The method should be empty
        'method' => '',
        // additional arguments sent to the mount() function
        'arguments' => [],
    ];
}

class LivewireComponentExample extends Component
{
    private Url $urlModel;
    private array $url_arguments;

    public function mount(Url $urlObj, $arguments = [])
    {
        $this->urlModel = $urlObj;
        $this->url_arguments = $arguments;
    }

    public function render()
    {
        return view('livewire.view-category');
    }
}


php artisan urls:generate [--model=ModelName] [--fresh] [--only-missing]

php artisan urls:doctor [--model=ModelName]
bash
php artisan vendor:publish --tag="laravel-unique-urls-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-unique-urls-config"