PHP code example of cliffordjames / laravel-urls

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

    

cliffordjames / laravel-urls example snippets


Route::get('/users/{user}', function (\App\User $user) {
    return $user;
})->name('users.show');



namespace App;

use CliffordJames\LaravelUrls\HasUrl;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasUrl;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

$user = User::first();
//
$user->url(); // -> /users/1
// vs
route('users.show', $user); // -> /users/1

Route::resource('users', 'UserController');

$user = User::first();

$user->url(); // -> /users/1
$user->url('show'); // -> /users/1
$user->url('edit'); // -> /users/1/edit
$user->url('update'); // -> /users/1
...

Route::get('/threads/{channel}/{thread}', 'ThreadsController@show')->name('threads.show');

$thread->url(); // -> /threads/*channel_id*/*thread_id*
$thread->url($thread->channel); // same as above
$thread->url($thread, $thread->channel); // same as above
$thread->url([$thread, $thread->channel]); // same as above
$thread->url(['thread' => $thread, 'channel' => $thread->channel]); // same as above

route('threads.show', ['thread' => $thread, 'channel' => $thread->channel]); // same as above

protected $baseRoute = 'profile';