Im developing a task system and i want to mark as viewed a task and do other things when a user request the task
I wrote a Task Event Subscriber like this
<?php namespace Athena\Events;
class TaskEventSubscriber {
public function onCreate($event)
{
// Here we can send a lot of emails
}
public function onUpdate($event)
{
\Log::info('This is some useful information.');
}
public function onShow($event)
{
\Log::info('The view event is now triggerd ');
}
public function subscribe($events)
{
$events->listen('user.create', 'Athena\Events\TaskEventSubscriber@onCreate');
$events->listen('user.update', 'Athena\Events\TaskEventSubscriber@onUpdate');
$events->listen('task.show', 'Athena\Events\TaskEventSubscriber@onShow');
}
}
And my controller I fire the event like this:
public function show($id)
{
$canView = $this->canView($id);
if($canView !== true)
{
return $canView;
}
$task = $this->task->byId($id);
// We fire the showed event
$this->events->fire('task.show', $this->task);
return View::make('tasks.show')
->with('task', $task);
}
But i dont know how can I or I should catch a parameter to be used inside of the event
By the way my Task Event subscriber is register on this Service Provider
class AthenaServiceProvider extends ServiceProvider {
public function register()
{
// A lot of stuffs
}
public function boot()
{
\Event::subscribe('Athena\Events\UserEventSubscriber');
\Event::subscribe('Athena\Events\TaskEventSubscriber');
}
}
If you need more information just let me know, Thnks in advance
I dont know how to feel already... I am passing the values what i need to my event there are on $event variable of each function
Note: if you need to send more than one value just put them into an array and set your event function to catch them like parameters
You will need to declare an Event object like this
<?php namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
class TriggerShowTask extends Event {
use SerializesModels;
public $task;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($task)
{
$this->task = $task;
}
}
When you trigger the event with
\Event::fire(TriggerShowTask, $this->task);
The $task
object will be passed to the event
You can then access it in the subscriber with
public function onShow($event)
{
$event->task; // A Task object
\Log::info('The view event is now triggerd ');
}