Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Laravel5.7事件侦听器中运行Javascript_Javascript_Laravel - Fatal编程技术网

在Laravel5.7事件侦听器中运行Javascript

在Laravel5.7事件侦听器中运行Javascript,javascript,laravel,Javascript,Laravel,我正在尝试在用户登录时运行javascript库Noty。我已经注册了该活动并遵循了文档。然而,我尝试使用echo发送javascript,但是代码没有按照我所看到的执行。工作流程是,在登录主页页面时,会弹出一条noty通知,表示欢迎 这是我的EventServiceProvider: class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the ap

我正在尝试在用户登录时运行javascript库Noty。我已经注册了该活动并遵循了文档。然而,我尝试使用echo发送javascript,但是代码没有按照我所看到的执行。工作流程是,在登录
主页
页面时,会弹出一条noty通知,表示欢迎

这是我的
EventServiceProvider

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'Illuminate\Auth\Events\Login' => ['App\Listeners\UserEventListener@onUserLogin']
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();


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

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

    }

    public function onUserLogin($event) {
         echo("<script>
            $(document).ready(function() {
                    new Noty({
                    text: 'Welcome {{Auth::user()->name}}! what you are trying to do doesn't seem possible, since the listener is not going to render any HTML page (where to code would be executed) but rather it will just execute some PHP code.

It seems to be a very strange approach for displaying messages with Laravel.

You could simply use this line In you login controller

return redirect('your/path')->with('success', 'your message here');

While somewhere at your blade template perform simple check:

@if (\Session::has('success'))
    <script>
            $(document).ready(function() {
                    new Noty({
                    text: 'Welcome {{ Auth::user()->name }}',
                    type:'info',
                    layout: 'centerRight',
                    timeout:2000
                })
                .on('onShow', function() {
                        var audio = new Audio('/sounds/appointed.mp3');
                        audio.play();
                    }).show();
            });
        </script>
@endif
UserEventListener

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'Illuminate\Auth\Events\Login' => ['App\Listeners\UserEventListener@onUserLogin']
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();


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

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

    }

    public function onUserLogin($event) {
         echo("<script>
            $(document).ready(function() {
                    new Noty({
                    text: 'Welcome {{Auth::user()->name}}! what you are trying to do doesn't seem possible, since the listener is not going to render any HTML page (where to code would be executed) but rather it will just execute some PHP code.

It seems to be a very strange approach for displaying messages with Laravel.

You could simply use this line In you login controller

return redirect('your/path')->with('success', 'your message here');

While somewhere at your blade template perform simple check:

@if (\Session::has('success'))
    <script>
            $(document).ready(function() {
                    new Noty({
                    text: 'Welcome {{ Auth::user()->name }}',
                    type:'info',
                    layout: 'centerRight',
                    timeout:2000
                })
                .on('onShow', function() {
                        var audio = new Audio('/sounds/appointed.mp3');
                        audio.play();
                    }).show();
            });
        </script>
@endif
类UserEventListener
{
/**
*创建事件侦听器。
*
*@返回无效
*/
公共函数构造()
{
//
}
/**
*处理事件。
*
*@param登录$event
*@返回无效
*/
公共函数句柄(登录$event)
{
}
公共函数onUserLogin($event){
回声(“
$(文档).ready(函数(){
新诺蒂({

text:'Welcome{{Auth::user()->name}}!您试图做的事情似乎不可能,因为侦听器不会呈现任何HTML页面(将在其中执行代码),而只会执行一些PHP代码。

使用Laravel显示消息似乎是一种非常奇怪的方法

您只需在登录控制器中使用这一行即可

return redirect('your/path')->带有('success','your message here');

在刀片模板的某个位置执行简单检查:

@if(\Session::has('success'))
$(文档).ready(函数(){
新诺蒂({
文本:“欢迎{{Auth::user()->name}”,
类型:'info',
布局:“中心右侧”,
超时:2000
})
.on('onShow',function(){
var audio=new audio('/sounds/committed.mp3');
音频播放();
}).show();
});
@恩迪夫

您可以将通知代码放在公共位置(例如布局)模板,所以如果用户会话中有消息,它将在任何地方发出通知

最初我想找到一种方法来做类似的事情,但是使用了一个自定义变量,但是找到了事件侦听器,这让我有点困惑,不知道从哪里开始。非常感谢!不客气。事件模式通常用于bri将架构组件放在一起,这样它们就可以在彼此不知道的情况下传递数据或触发彼此的逻辑