问题
The main Laravel application object has two methods, booting
and booted
. These methods allow you to configure callbacks. The application object will call these callbacks before and after it boots.
Where, as a Laravel application developer, can I hook into these events? Looking at the framework it seems like
bootstrap/start.php
is the obvious place — but if I put code here it'll be zapped in the next update. There's also
start/global.php
statt/{$env}.php
but these files are actually required in by a booted
callback, which means the booting
callback point would be unavailable.
Is there an intended place for me to hook into these events as a Laravel application developer? Or are booting
and booted
there for the core framework developers? Or has I made an incorrect assumption that I'm not even aware of? Laravel 4.2.6, but if there's context from additional versions I'd love to hear about it.
New to Laravel, not new to programming. Not asking for a specific task (yet), just getting a feel for what best practices/intended use is and is not.
回答1:
As a formal answer, there's no defined place for this behaviour. But a note against your comments about start.php
, this file will not be "zapped" when you update laravel.
As long as a file is not within the vendors folder, any laravel updates won't change your files. start.php
is generated when you create a base project, but should not be changed with further updates.
composer create-project
is responsible for creating your base "skeleton" application, and any composer update
that you do will just modify files in the vendor folder. Just remember, do not modify files in the vendor folder and you will be fine!
来源:https://stackoverflow.com/questions/24856157/laravel-where-to-add-booted-and-booting-callbacks