Package Data | |
---|---|
Maintainer Username: | tsommie |
Maintainer Contact: | anitchec.dev@gmail.com (Anitche Chisom) |
Package Create Date: | 2018-06-06 |
Package Last Update: | 2019-04-16 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-24 15:07:22 |
Package Statistics | |
---|---|
Total Downloads: | 12 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 0 |
This is a simple presenter much like the Laracast Presenter but specifically tailored for presenting urls in Laravel as suggested by a post on laravel-news.com writen by Jordan Dalton.
Add the Laravel Form Processor package to your composer.json
file.
composer require ac-developers/eloquent-url-presenter
Auto-discovery: Is supported in Laravel Form Processor auto-discovery for Laravel 5.5 and greater.
In your app config, add the EloquentUrlPresenterServiceProvider
to the providers array.
'providers' => [
AcDevelopers\EloquentUrlPresenter\EloquentUrlPresenterServiceProvider::class,
];
For Lumen, add the provider to your bootstrap/app.php
file.
$app->register(AcDevelopers\EloquentUrlPresenter\EloquentUrlPresenterServiceProvider::class);
To publish the config file to config/eloquent-url-presenter.php
run:
php artisan vendor:publish --provider="AcDevelopers\EloquentUrlPresenter\EloquentUrlPresenterServiceProvider"
To change the paths of saving the generated url presenters, you need to configure their namespaces in a configuration file config/ac-developers/eloquent-url-presenter.php
.
return [
/*
|--------------------------------------------------------------------------
| Default namespaces for the classes
|--------------------------------------------------------------------------
*/
'namespaces' => [
'presenter' => 'App\Presenters\Url',
'model' => 'App\\',
],
];
After this your good to go.
Creating a url presenter class is as easy as creating any other php class with just a few steps required to make it url presentable. In our case we will create a UserUrlPresenter class.
first it extends our EloquentUrlPresenterClass. Next you pass in the eloquent model that would be making use of the newly created url presenter in to it's constructor like this:
/**
* UserUrlPresenter constructor.
*
* @param \App\User $user
*/
public function __construct(User $user)
{
parent::__construct($user);
//
}
Then create the method that would return the desired url.
/**
* Get the show url for this user
*
* @return string
*/
public function show()
{
return url('show/{user}', ['slug' => $this->entity])
}
If your on Laravel or Lumen and you want to make this presenter resoucesful without manually adding the methods one by one you just have to add the HasResource
trait to the UrlPresenter and implement the route
method which should return a string similar to that passed in as first argument in the Route::resouce
method and the parameter
method which does exactly what you would expect.
So if in your route method looks like Route::resource('users', 'UserController')
then the route
method implemented in the UserUrlPresenter should return users
else laravel will throw an exception.
And the next is to prepare our model to use our url presenter.
Preparing the model that would use the url presenter is simple, Just use the EloquentUrlPresentableTrait and then implementing the urlPresenter methord which will return our UserUrlPresenter class
/**
* Prepare a new or cached url presenter instance
*
* @return mixed
*/
public function urlPresenter()
{
return UserUrlPresenter::class;
}
Next we will add "url" to our appends property array like this:
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['url'];
Next you add url
as an array value in our model's appends
property and then your done.
Now in our code we can link to our user model show page like this:
$user->url->show
Lets say your using the HasResource
trait you'll automatically have access to all resourcful route methods and these includes index
, create
, show
, edit
, store
, update
and destroy
**Note: This is to be used only in Laravel and Lumen applications
Ofcause the following steps narrated previously was to show you how to do it yourself. You can skip this and just run the php artisan generate:urlPresenter
command. For example to create our UserUrlPresenter
we would do like this:
php artisan generate:urlPresenter UserUrlPresenter --model=User
To make a resourceful url presenter all you have to do is pass the resouces
option while generating the url presenter without a value like so:
php artisan generate:urlPresenter UserUrlPresenter --model=User --resource
If you discover a security vulnerability within Laravel Form Processor, please send an e-mail to Anitche Chisom via anitchec.dev@gmail.com. All security vulnerabilities will be promptly addressed.
The Eloquent Url Presenter is open-sourced software licensed under the MIT license.