4

New to symfony, I am trying to start my first session

here is my entire code situated under public/php/session.php:

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

$session = new Session();
$session->start();

I am getting error Uncaught Error: Class 'Symfony\Component\HttpFoundation\Session\Session' not found

phpstorm gives no error, do I need to install a module ? I tried composer require session but that does not work

I also tried the symfony doc method with handler_id: ~ in config/packages/framework.yaml

with this method, no error message but no session cookie either

and here is my controller:

class HomeController extends AbstractController {

 // start session

  public function index(SessionInterface $session) {
   $session->set('foo', 'bar');
   $session->get('foo');
  }

  /**
  * @Route("/", name="home")
  */

  public function homepage(){
   return $this->render('home.html.twig');
  }

 }
Will B.
  • 17,883
  • 4
  • 67
  • 69
Sam
  • 1,557
  • 3
  • 26
  • 51
  • Try using `php composer.phar dump-autoload -o`, see my answer on [Composer & Linux production server - autoload not working](https://stackoverflow.com/a/39212457/1144627) Typically you would want to make a service in `/src` that you pass to your controller, as opposed to putting into your `/public` path. – Will B. Aug 08 '18 at 12:46
  • trying the command from my website folder I get `Could not open input file: composer.phar` – Sam Aug 08 '18 at 12:58
  • Curious as to how you are calling `public/php/session.php`, don't think the `composer dump-autoload -o` trick will work unless you are loading it from within the controller, since `session.php` does not include the autoloader to benefit from the `-o` flag. – Will B. Aug 08 '18 at 13:00
  • Well, Im back to the official method now(as described in the question); no error but not getting any session cookie – Sam Aug 08 '18 at 13:05
  • Your index method is not returning any response so no cookie will be sent back. – Cerad Aug 08 '18 at 13:07
  • @Cerad but the symfony doc does not mention returning anything ? I tried `return $session` and `return $this->session` without any luck – Sam Aug 08 '18 at 13:26
  • It is basic http protocol. Consider ditching Symfony for the moment and write a few basic examples maybe following the [php docs](http://php.net/manual/en/book.session.php). Once you get a basic idea of how servers and browsers and cookies and sessions interact then it will become clear. – Cerad Aug 08 '18 at 13:29

2 Answers2

1

If you use whole symfony framework it starts the sessions are automatically started whenever you read, write or even check for the existence of data in the session. You don't need to do that manually.

What you need to do is to define an adapter that you'll use or leave it to php configruation in

config/packages/framework.yaml

framework:
+     session:
+         # The native PHP session handler will be used
+         handler_id: ~

and then in your services, controlers just get session service

with SF4 and auto wiring enabled, in controller action

public function index(SessionInterface $session)
{
       $session->set('foo', 'bar');
       $session->get('foo');
}

that's all. See more in https://symfony.com/doc/current/controller.html#session-intro


Your code is not within the framework and it won't work because there's no autoloader which would load the component from composer, it'd work if you included vendor/autoload.php but don't go that way.

Robert
  • 19,800
  • 5
  • 55
  • 85
  • Sorry I forgot to mention, I tried that too. I put it inside my home controller; The website loads but I have no session cookie in my browser ?? The symfony doc says the session should be created with the `set`? I'll update my question – Sam Aug 08 '18 at 12:38
  • check your PHP session configuraiton then f.e in php.ini because when you set handler as ~ then symfony just uses native session configration from php – Robert Aug 08 '18 at 13:17
  • I have no php.ini in my app anywhere. if I just put `session_start()` the simple php way it works just fine – Sam Aug 08 '18 at 13:28
  • 1
    if so then put what I gave you in the config and clear cache it should work. Make sure you don't start session on your own – Robert Aug 08 '18 at 13:29
1

As mentioned by @Robert, since your code is within public/php it is not aware of the autoloader, which tells PHP where files are located in relation to their namespaces (PSR-0 or PSR-4).

I believe the confusion is that your public index function does not automatically start the session, as it is not called by Symfony unless you navigate to the index() function and must return a Response. Try passing the session to your homepage method arguments and navigating to it in the browser.

use Symfony\Component\HttpFoundation\Session\SessionInterface;

class HomeController extends AbstractController 
{

  /**
   * @Route("/", name="home")
   */
  public function homepage(SessionInterface $session)
  {
    $session->set('foo', 'bar');

    return $this->render('home.html.twig');
  }

 }
Will B.
  • 17,883
  • 4
  • 67
  • 69