D
D
Dmitry2016-01-25 15:44:26
Laravel
Dmitry, 2016-01-25 15:44:26

How to implement event listening or how to solve the problem in general?

There is a self-written admin panel on Lara. It works on two controllers - AdminController and EntityController. The first is system pages, such as configuration, export / import of data to the database, and so on. The second one is working with entities, for example, news, catalog and the like (list, editing, copying, deleting, saving, etc).
The parameters of each entity are stored in the configuration file (this is not an important point).
How to implement event listening so that, for example, when creating a record of a certain entity (for example, creating a new news item), a certain code (in a separate controller?) is executed.
The idea is this - when creating a news item, you need to check if there is a "Crossposting" checkbox, and if it is, you need to transfer the news model with the filled fields to the controller, for example, [email protected], where additional manipulations will already be performed.
I will be grateful for any help.
Thank you gentlemen.
Update:
Okay, I figured out the event trapping.
In the News model, we have the following code:

public static function boot()
    {
        parent::boot();

        static::creating(function($news)
        {
            dd('Create new item');
        });
    }

How to move the code from static::creating(function($news) { ... }) to a separate controller? For example, in [email protected]
Will the next option work?
// App/Models/News
public static function boot()
    {
        parent::boot();

        static::creating(function($news)
        {
            Event::fire('news.createcrosspost', array($news));
        });
    }
// App/Providers/EventServiceProvider

protected $listen = [
    'news.createcrosspost' => [
      '[email protected]',
    ],
  ];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pochepko, 2016-01-25
@another_dream

Doc, take a closer look. Even in Russian there is Tyk

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question