PHP code example of echosters / routable
1. Go to this page and download the library: Download echosters/routable 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/ */
echosters / routable example snippets
'providers' => [
...,
Echosters\Routable\Providers\RoutableServiceProvider::class,
];
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Echosters\Routable\Routable;
class YourModel extends Model
{
use Routable;
}
$user = User::find(1);
$user->getRoute('edit'); // the same of route('users.edit',$user->id)
User::find(1)->getRoutes();
Array
[
"edit" => "http://example.com/users/1/edit"
"update" => "http://example.com/users/1"
"show" => "http://example.com/users/1"
"destroy" => "http://example.com/users/1"
"store" => "http://example.com/users"
"index" => "http://example.com/users"
]
User::find(1)->getRoute('show');
string
"http://example.com/users/1"
User::getResourceRoute('index');
string
"http://example.com/users"
User::find(1)->getRoute('show',['highlight' => 'true']); // "http://example.com/users/1?highlight=true"
User::find(1)->getRoutes('show',['highlight' => 'false']); // "http://example.com/users/1?highlight=false"
User::getResourceRoute('index',['id' => '1']); // "http://example.com/users?id=1"
public function getRouteName()
{
return 'custom-users';
}
//Then
$user = User::find(1);
$user->getRoute('edit'); //will generate 'http://example.com/en/custom-users/1'
public function getFixedParameter()
{
return [
'locale' => 'en',
];
}
//Then
$user = User::find(1);
$user->getRoute('edit'); //will generate 'http://example.com/en/users/1'
public function user()
{
return $this->belongsTo(User::class);
}
Route::resource('{user}/posts',PostsController::class);
public function getFixedParameter()
{
return [
'user' => $this->user->id,// Or $this->user,
];
}
//Then
$post = Post::find(1); // Known that $post->user->id = 22;
$post->getRoute('show'); //will generate 'http://example.com/22/posts/1'
config\app.php
App\Models\Post.php
App\Models\Post.php