1. Go to this page and download the library: Download sarfraznawaz2005/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/ */
sarfraznawaz2005 / actions example snippets
class PublishPostAction extends Action
{
/**
* Define any validation rules.
*/
protected $rules = [];
/**
* Perform the action.
*
* @return mixed
*/
public function __invoke()
{
// code
}
}
// routes/web.php
Route::get('post', '\App\Http\Actions\PublishPostAction');
// or
Route::get('post', '\\' . PublishPostAction::class);
$action = new PublishPostAction();
$action();
class TodosListAction extends Action
{
protected $todos;
public function __invoke(Todo $todos)
{
$this->todos = $todos->all();
}
protected function html()
{
return view('index')->with('todos', $this->todos);
}
protected function json()
{
return TodosResource::collection($this->todos);
}
}
class TodosListAction extends Action
{
protected $todos;
public function __invoke(Todo $todos)
{
$this->todos = $todos->all();
}
protected function html()
{
return view('index')->with('todos', $this->todos);
}
protected function json()
{
return TodosResource::collection($this->todos);
}
public function isApi()
{
return request()->wantsJson() && !request()->acceptsHtml();
}
}