PHP code example of cybercog / laravel-optimus

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

    

cybercog / laravel-optimus example snippets


'providers' => [
    Cog\Laravel\Optimus\Providers\OptimusServiceProvider::class,
],

'aliases' => [
    'Optimus' => Cog\Laravel\Optimus\Facades\Optimus::class,
],
 
Cog\Laravel\Optimus\Facades\Optimus::encode(20); // 1535832388

Cog\Laravel\Optimus\Facades\Optimus::decode(1535832388); // 20

use Cog\Laravel\Optimus\Facades\Optimus;

// Writing this…
Optimus::connection('main')->encode($id);

// …is identical to writing this
Optimus::encode($id);

// and is also identical to writing this.
Optimus::connection()->encode($id);

// This is because the main connection is configured to be the default.
Optimus::getDefaultConnection(); // This will return main.

// We can change the default connection.
Optimus::setDefaultConnection('alternative'); // The default is now alternative.

use Cog\Laravel\Optimus\OptimusManager;

class Foo
{
    protected $optimus;

    public function __construct(OptimusManager $optimus)
    {
        $this->optimus = $optimus;
    }
    
    public function bar($id)
    {
        return $this->optimus->encode($id)
    }
}

app()->make('Foo')->bar(20);

use Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model
{
    use OptimusEncodedRouteKey;
    
    protected $optimusConnection = 'custom'; // optional
}

Route::get('url/to/{model}', function (YourModel $model) {
    // ...
})->middleware('web');

$encodedId = $model->getRouteKey();
$url = url("url/to/{$encodedId}");

$url = route('my.named.route', $model);
shell
php artisan vendor:publish --provider="Cog\Laravel\Optimus\Providers\OptimusServiceProvider" --tag="config"
shell
php vendor/bin/optimus spark