未找到Laravel事件

时间:2018-01-31 05:06:34

标签: php laravel laravel-mail laravel-events

我尝试在用户提交新订单时通过pusher发送通知。

在我的CartController我将数据保存到我的订单表中,我有:

<?php

use App\Events\UserOrdered;

//codes....
        public function checkout(Request $request) {

        //rest of the code

        Auth::user()->orders()->save($order);

        event(new UserOrdered($order));

        //Cart::clear();
        Session::flash('success', 'Thank you. Your order has been received.');
        return redirect()->route('ordersindex');
    }

当我点击保存订单时,我会在404 NOT FOUND

上获得event(new UserOrdered($order));

以下是我的Event代码:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Order;

class UserOrdered implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $order;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('order-placed');
    }
}

这是我的Listener代码:

<?php

namespace App\Listeners;

use App\Events\UserOrdered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    /**
     * Handle the event.
     *
     * @param  UserOrdered  $event
     * @return void
     */
    public function handle(UserOrdered $event)
    {
        $pusher->trigger($event, 'order-placed', $order);
    }
}
  

PS:当我从推送网站Debug Console尝试我的推送器时,我可以收到通知。无论出现什么问题,都来自事件和听众。

有什么想法吗?

更新

screen1

更新2

我设法推动工作(我知道这不是最好的方式,但至少可以起作用。)

现在是我的听众:

<?php

namespace App\Listeners;

use App\Events\UserOrdered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Pusher\Pusher;
use Mail;


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

    /**
     * Handle the event.
     *
     * @param  UserOrdered  $event
     * @return void
     */
    public function handle(UserOrdered $event)
    {
        Mail::to($event->order->buyer_email)->send(new UserOrdered($event->order));

        $options = array(
            'cluster' => 'ap1', 
            'encrypted' => true
        );
        $pusher = new Pusher(
            'xxxxx', //id
            'xxxxx', //key
            'xxxx', //secret_key
            $options
        );
        $pusher->trigger('UserOrdered', 'order-placed', $event->order);
    }
}

我需要什么?

  1. Pusher会在管理员显示新订单时显示通知。 done
  2. 我还想向管理员发送2封电子邮件,说他/她有新的 订单,另一个用户说我们收到他/她的订单。
  3. 邮件功能:

    <?php
    
    namespace App\Mail;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use App\Order;
    
    class UserOrdered extends Mailable
    {
        use Queueable, SerializesModels;
        public $order;
    
        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct(Order $order)
        {
            $this->order = $order;
        }
    
        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            return $this->from('noreply@tester.com')->subject('Your Order recieved!')->markdown('emails.orders');
        }
    }
    

    进展:为了发送电子邮件,我在上面的监听器中添加了Mail::to,并将下面的代码添加到我的控制器功能中:

    //rest of the code...
          Auth::user()->orders()->save($order);
    
          $user = $order->buyer_email;
    
          event(new UserOrdered($order));
          Mail::to($request->user)->send(new UserOrdered($order)); //added newly 
    
          //Cart::clear();
          Session::flash('success', 'Thank you. Your order has been received.');
          return redirect()->route('ordersindex');
    

    问题是:

    我收到此错误

      

    类型错误:参数1传递给Illuminate \ Mail \ PendingMail :: send()   必须是Illuminate \ Mail \ Mailable的实例,实例   给定的App \ Events \ UserOrdered

    指的是:Mail::to($event->order->buyer_email)->send(new UserOrdered($event->order));

    如果我将$event->order更改为$event之类的静态内容,我会得到:

      

    类型错误:参数1传递给App \ Events \ UserOrdered :: __ construct()   必须是App \ Order的实例,App \ Events \ UserOrdered的实例   给定

    任何想法?

0 个答案:

没有答案