1. Go to this page and download the library: Download devio/permalink 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/ */
devio / permalink example snippets
$router->get('users', 'UserController@index');
$router->get('users/israel-ortuno', 'UserController@show');
// Which will produce:
// /users UserController@index
// /users/israel-ortuno
// Note: when using the User::create method, even if permalinkHandling (read more about it below)
// is disabled, it will create the permalink record.
$user = User::create([
'name' => 'israel',
'permalink' => [
'slug' => 'israel-ortuno',
'action' => 'user.show',
'seo' => [...] // omit this attribute until you read more about it
]
]);
// Or
$user->createPermalink([...);
class User extends Model implements \Devio\Permalink\Contracts\Permalinkable;
{
use \Devio\Permalink\HasPermalinks;
public function permalinkAction()
{
return UserController::class . '@show';
}
public function permalinkSlug(): array
{
return ['entity.name'];
}
}
public function show($user)
{
return view('users.show', $user);
}
// Temporally disable/enable:
$model->disablePermalinkHandling();
$model->enablePermalinkHandling();
// Permanent disable or return a condition.
// Create this method in you model:
public function permalinkHanlding()
{
return false;
}
// Assume permalinkHanlding() returns false
$user = User::create(['name' => 'israel']);
// Perform other tasks...
$user->createPermalink(); // Array is optional, provide data to override default values
// Globally disable this feature for all models in your permalink.php config file
'nest_to_parent_on_create' => false
// or
config()->set('permalink.nest_to_parent_on_create', false);
// Disable this feature for a particular model. Define this method in your model class:
public function permalinkNestToParentOnCreate()
{
return false;
}
Permalink::create(['slug' => 'israel-ortuno', 'entity_type' => User::class, 'entity_id' => 1, 'action' => 'users.show']);
// And then in users/show.blade.php
<h1>Welcome {{ $user->name }}</h1>
// In your AppServiceProvider.php
public function register()
{
$this->bind('Devio\Permalink\Http\PermalinkController', YourController::class);
}
// And then...
class YourController
{
use Devio\Permalink\Http\ResolvesPermalinkView;
public function __construct()
{
// Do your stuff.
}
}
public function permalinkAction()
{
return UserController::class . '@show'; // Or a view
}
class User extends Model
{
use HasPermalinks;
...
public function permalinkAction()
{
return UserController::class . '@index';
}
...
}
// And then...
$user = User::create([
'name' => 'israel',
'permalink' => [
'action' => 'user.show'
]
]);
// Or just update the action attribute as you like
Peramlink::create([
'slug' => 'foo',
'seo' => [
'title' => 'this is a title',
'description' => 'this is a description',
'opengraph' => [
'title' => 'this is a custom title for og:title'
]
]
);
public function getPeramlinkSeoTitleAttribute()
{
return $this->name;
}
public function getPermalinkSeoOpenGraphTitleAttribute()
{
return $this->name . ' for OpenGraph';
}
// Singleton or not, whatever you meta", function ($app) { // meta, opengraph, twitter or base
return new MyCustomBuilder;
// Or if you are inheriting the default builder class
return (new MyCustomBuilder($app->make(SeoHelper::class)));
});