dtz88967 2019-01-06 21:06
浏览 153

如何在Laravel中制作Controller Singleton?

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. The routes are specified in the web.php file like so:

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).

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. 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.

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.

Is it possible?

  • 写回答

1条回答 默认 最新

  • douqiu9529 2019-04-12 08:57
    关注

    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.

    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. 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

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试