0

How can make sure session is always started in Yii2? In Yii 1.0 there seemed to be a configuratio parameter autoStart but I don't see that in Yii2.

Do I need to manually call Yii::$app->session->open() (or session_start()), and if so, where is a good place to do that for all requests?

At the time of writing The Definitive Guide doesn't talk about it.

TheStoryCoder
  • 3,403
  • 6
  • 34
  • 64

1 Answers1

3

Every time you call method processing session data in Yii 2, session is started automatically if it hasn't started already.

You can always call Yii::$app->session->open() directly if you want.

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • That's not what I'm asking - the guide explains that part just fine. I don't want to always use `Yii::$app->session`, just the normal $_SESSION['x'] because it's shorter. Therefore I just need to know how to make sure the session is always started automatically. – TheStoryCoder Oct 25 '16 at 12:31
  • You can put it in a common class like a common controller that all other controllers are extending. I don't recommend accessing plain $_SESSION - all features of this component are lost in this case. You can always shorten `Yii::$app->session` to `Yii::$app->s` in configuration if this is a problem. – Bizley Oct 25 '16 at 12:44
  • Extending all controllers from a custom base controller sounds like a messy solution - eg. for developers who aren't aware they need to do that. -- I don't see any actual value-added features of this component - rather, the guide talks about one disadvantage. And I know I don't need custom storage. But I'm perfectly willing to be enlightened if I overlooked something... – TheStoryCoder Oct 25 '16 at 12:49
  • Base controller is quite common solution. And developers don't need to do it - it's a solution to force start session you requested. Primary feature of this component is its construction - it allows using the features of Iterator and ArrayAccess. Also custom storage (even if you don't need it). Using the component allows to set and change configuration easily, prepare variations, add parameters etc. Let say for some reason you need to change all SESSION key names to have the same prefix. With component it's super easy - without it you have to change it in every piece of code. – Bizley Oct 25 '16 at 13:01
  • Wouldn't developers have to extend from something else than `yii\web\Controller`?? -- I don't see how those really add any actual value in real-life. In the extremely rare case I'd have to prefix SESSION key names it's easy to do a search and replace on $_SESSION[]. – TheStoryCoder Oct 25 '16 at 13:44
  • It can be basically any class/method that is called during the script life cycle before sending the headers. I don't know what to say more to convince you. You can always use plain PHP anywhere but what is the point of using framework then? And this search-replace argument here is like saying there is no point of using plug to drain a bathtube when you can do it with one simple glass. – Bizley Oct 25 '16 at 14:11
  • The point of a framework is to simplify things, write less code and encourage a certain structure. But if the features don't add any real advantages they just become an extra, useless layer the developer has to waste his time learning in order to get things done. Besides, I can't see why it would be an issue to have an auto-start session option. Anyway, a workaround is to add `session_start()` to `/web/index.php` – TheStoryCoder Oct 25 '16 at 14:20
  • 3
    But Yii 2 **is doing exactly that** - it allows you to use session data without need to worry about opening the session. I'm afraid you are bravely fighting the problem you created in the first place. – Bizley Oct 25 '16 at 14:41
  • `Yii::$app->session['x'];` that's all. You don't have to call `Yii::$app->session->open()` anywhere. Because, in Yii2 session always start when you call `Yii::$app->session` – Ngô Văn Thao Oct 27 '16 at 02:06