1. Go to this page and download the library: Download larapie/actions 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/ */
larapie / actions example snippets
// app/Actions/PublishANewArticle.php
class PublishANewArticle extends Action
{
public function authorize()
{
return $this->user()->hasRole('author');
}
public function rules()
{
return [
'title' => '
$action = new PublishANewArticle([
'title' => 'My blog post',
'body' => 'Lorem ipsum.',
]);
$article = $action->run();
class ProductCreated
{
public $title;
public $body;
public function __construct($title, $body)
{
$this->title = $title;
$this->body = $body;
}
}
Event::listen(ProductCreated::class, PublishANewArticle::class);
event(new ProductCreated('My new SaaS application', 'Lorem Ipsum.'));
class PublishANewArticle extends Action
{
// ...
public function response($article)
{
return redirect()->route('article.show', $article);
}
}
$action = new Action(['key' => 'value']); // Initialise an action with the provided attribute.
$action->fill(['key' => 'value']); // Merge the new attributes with the existing attributes.
$action->all(); // Retrieve all attributes of an action as an array.
$action->only('title', 'body'); // Retrieve only the attributes provided.
$action->except('body'); // Retrieve all attributes excepts the one provided.
$action->has('title'); // Whether the action has the provided attribute.
$action->get('title'); // Get an attribute.
$action->get('title', 'Untitled'); // Get an attribute with default value.
$action->set('title', 'My blog post'); // Set an attribute.
$action->title; // Get an attribute.
$action->title = 'My blog post'; // Set an attribute.
// Resolved from the IoC container.
public function handle(Request $request) {/* ... */}
public function handle(MyService $service) {/* ... */}
// Resolved from the attributes.
// -- $title and $body are equivalent to $action->title and $action->body
// -- When attributes are missing, null will be returned unless a default value is provided.
public function handle($title, $body) {/* ... */}
public function handle($title, $body = 'default') {/* ... */}
// Resolved from the attributes using route model binding.
// -- If $action->comment is already an instance of Comment, it provides it.
// -- If $action->comment is an id, it will provide the right instance of Comment from the database or fail.
// -- This will also update $action->comment to be that instance.
public function handle(Comment $comment) {/* ... */}
// They can all be combined.
public function handle($title, Comment $comment, MyService $service) {/* ... */}
public function authorize()
{
// Your authorisation logic here...
}
public function authorize()
{
return $this->user()->isAdmin();
}
$action->actingAs($admin)->run();
public function authorize()
{
return $this->can('create', Article::class);
}
public function rules()
{
return [
'title' => '
public function withValidator($validator)
{
$validator->after(function ($validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
});
}
public function afterValidator($validator)
{
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
};
}
public function handle()
{
$this->validate([
'comment' => '
$action = new PublishANewArticle;
$action->title = 'My blog post';
$action->set('body', 'Lorem ipsum.');
$action->run();
(new PublishANewArticle)->run([
'title' => 'My blog post',
'body' => 'Lorem ipsum.',
]);
public function register()
{
$this->middleware('auth');
}
public function response($result, $request)
{
return view('articles.index', [
'articles' => $result,
])
}
public function htmlResponse($result, $request)
{
return view('articles.index', [
'articles' => $result,
]);
}
public function jsonResponse($result, $request)
{
return ArticleResource::collection($result);
}
public function handle()
{
$this->runningAs('object');
$this->runningAs('job');
$this->runningAs('listener');
$this->runningAs('controller');
// Returns true of any of them is true.
$this->runningAs('object', 'job');
}
public function asController(Request $request)
{
$this->token = $request->cookie('token');
}
class CreateNewRestaurant extends Action
{
public function handle()
{
$coordinates = (new FetchGoogleMapsCoordinates)->run([
'address' => $this->address,
]);
return Restaurant::create([
'name' => $this->name,
'longitude' => $coordinates->longitude,
'latitude' => $coordinates->latitude,
]);
}
}
class UpdateProfile extends Action
{
public function handle()
{
if ($this->has('avatar')) {
return $this->delegateTo(UpdateProfilePicture::class);
}
if ($this->has('password')) {
return $this->delegateTo(UpdatePassword::class);
}
return $this->delegateTo(UpdateProfileDetails::class);
}
}
// These two lines are equivalent.
$this->delegateTo(UpdatePassword::class);
UpdatePassword::createFrom($this)->runAs($this);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.