简体   繁体   English

如何在Laravel中创建Controller Singleton?

[英]How to make a Controller Singleton in Laravel?

I'm making an airlines web app. 我正在制作航空公司的网络应用程序。 Right now I'm coding de process of reserving a flight, for that purpose I have multiple views such as reserve.blade.php , chooseFlights.blade.php , passengersInfo.blade.php , etc. All those views use the same ReserveController class. 现在,我的编码保留飞行的过程中去,为此我有诸如多个视图reserve.blade.phpchooseFlights.blade.phppassengersInfo.blade.php ,等等。所有这些观点使用相同的ReserveController类。 The routes are specified in the web.php file like so: 路由在web.php文件中指定,如下web.php

Route::get('/reserve', 'ReserveController@searchFlights');
Route::get('/reserve/choose_flights', 'ReserveController@chooseFlights');
Route::post('/reserve/storeFlightsIds', 'ReserveController@storeFlightsIds');
Route::get('/reserve/passengers_info', 'ReserveController@retrievePassengersInfo');

Where those methods return the respective views with the needed data to be displayed. 这些方法在其中返回各自的视图以及要显示的所需数据。

The workflow of reserving is /reserve (returns the view reserve.blade.php ) that view makes a get request to -> /reserve/choose_flights (returns the view chooseFlights.blade.php ) that view makes a post request to -> /reserve/storeFlightsIds (returns the view passengersInfo.blade.php ). 保留的工作流程是/reserve (返回视图reserve.blade.php ),该视图向-> /reserve/choose_flights (返回视图chooseFlights.blade.php ),该视图向-> /reserve/storeFlightsIds (返回视图passengersInfo.blade.php )。

In reserve.blade.php info like the origin and destiny of the desired flight, the dates and number of passengers is sent to the controller. reserve.blade.php信息中,例如所需航班的始发地和目的地,将乘客的日期和人数发送到控制器。 The problem is the number of passengers is required steps further in the passengersInfo.blade.php for the view to know how many forms has to display to retrieve the info of all the passengers. 问题是乘客的数量所需的步骤在进一步passengersInfo.blade.php的知道有多少形式能够显示检索所有乘客的信息视图。

I would like to use the same instance of the ReserveController whenever a request involving it is accessed so that way I can store the needed data in properties and share the same data corresponding to the current reserve process across all those views. 每当访问涉及它的请求时,我ReserveController使用ReserveController的同一实例,这样我就可以在属性中存储所需的数据,并在所有这些视图中共享与当前储备过程相对应的相同数据。

Is it possible? 可能吗?

I would like to use the same instance of the ReserveController whenever a request involving it is accessed so that way I can store the needed data in properties and share the same data corresponding to the current reserve process across all those views. 每当访问涉及它的请求时,我都想使用ReserveController的同一实例,这样我就可以在属性中存储所需的数据,并在所有这些视图中共享与当前储备过程相对应的相同数据。

Is it possible? 可能吗?

Yes its possible, you register a singleton instance in your app's service container 是的,您可以在应用程序的服务容器中注册一个单例实例

$this->app->singleton('MySingleton', function ($app) {
     return new SomeSharedClass(...);
});

and then you store the shared state inside that class. 然后将共享状态存储在该类中。 And inject it in your controller. 并将其注入您的控制器中。

class ReserveController extends Controller 
{
     private $singleton;

     function __construct(MySingleton $singleton) {
         $this->singleton = $singleton;
     }

     ...
}

Warning!! 警告!! This is not recommended. 不建议这样做。 Remember if 2 different users will call the same controller method, they will also share the singleton instances. 请记住,如果2个不同的用户将调用相同的控制器方法,那么他们还将共享单例实例。 shared state is evil! 共享状态是邪恶的!

Read more about shared states here. 在此处阅读有关共享状态的更多信息。 http://henrikeichenhardt.blogspot.com/2013/06/why-shared-mutable-state-is-root-of-all.html http://henrikeichenhardt.blogspot.com/2013/06/why-shared-mutable-state-is-root-of-all.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM