Yasmin French
Yasmin French

Reputation: 1284

Get variables from new Event Laravel

I'm using laravel to handle events, specifically when a user accesses a certain page, my code is as follows:

This is my PageController:

    public function showTestPage()
    {
        //This "redirect to" method is so i don't have to rewrite the event
          for every method that returns a new view;
        $this->redirectTo('test');
    }

    public function redirectTo($page)
    {
        //This is the data i want to access from my UserAccessedPage Listener
        event(new UserAccessedPage($page));
        return view($page);
    }

Lastly here is my UserAccessedPage Listener:

class LogUserAccessedPage {

/**
 * Create the event listener.
 *
 * @return void
 */
public function __construct()
{

}

/**
 * Handle the event.
 *
 * @param  UserAccessedPage  $event
 * @return void
 */
public function handle(UserAccessedPage $event)
{

    Log::create([
        'info' => "User ". ucfirst(Auth::user()->name) . " Accessed Page " . Carbon::now()->format('H:i:s'),
        'user' => ucfirst(Auth::user()->name),
        'object_affected' => Auth::user()->id,
        'ip_address' => '',
        'time' => Carbon::now()->format('H:i:s'),
        'date' => Carbon::now()->format('Y-m-d')
    ]);
}

}

How to i get that $page variable to my listener so i can put it in the Log::create(['info' => $page]), like that? I hope this makes sense what im asking

As requested the code for my UserAccessedPage:

class UserAccessedPage extends Event

{
    use SerializesModels;

    public $page;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($page)
    {
        $this->page = $page;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }

My EventServiceProvider to clear up some confusion

protected $listen = [
    'App\Events\UserLoggedIn' => [
        'App\Listeners\LogUserLoggedIn',
    ],
    'App\Events\UserLoggedOut' => [
        'App\Listeners\LogUserLoggedOut',
    ],
    'App\Events\UserAccessedPage' => [
        'App\Listeners\LogUserAccessedPage',
    ],
];

Upvotes: 1

Views: 1546

Answers (2)

Yasmin French
Yasmin French

Reputation: 1284

I figured it out im so silly -_-

because i had this piece of code public function handle(UserAccessedPage $event) it meant that the class "UserAccessedPage" could be access through the variable "$event" so my final solution was simply using $event->page to echo out the page variable.

Thank all of you for your help :)

Upvotes: 1

Carlos
Carlos

Reputation: 1331

Try using Setters and Getters. On your events page, do the following:

public $page

public function __construct($page)
{
    $this->setPage($page);
}

public function setPage($page) {
    $this->page = $page;
}

public function getPage() {
    return $this->page;
}

On your Listener, get the page:

public function handle(UserAccessedPage $event)
{
    $page = $event->getPage();

    Log::create([
        'info' => "User ". ucfirst(Auth::user()->name) . " Accessed Page " . Carbon::now()->format('H:i:s'),
        'user' => ucfirst(Auth::user()->name),
        'object_affected' => Auth::user()->id,
        'ip_address' => '',
        'time' => Carbon::now()->format('H:i:s'),
        'date' => Carbon::now()->format('Y-m-d')
    ]);
}

Upvotes: 1

Related Questions