Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7972181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:47:54+00:00 2026-06-04T07:47:54+00:00

if you want to access the Session from a Controller you can use this->get(‘session’);

  • 0

if you want to access the Session from a Controller you can use this->get('session');.

But How does it work from within an Extension?

I tried configuring it within the services.xml and tweaking the __construct($session) method:

<service id="myService" class="%myServie.class%">
    <argument type="service" id="session"/>
</service>

didn’t work. (null-object)

I tried reaching it from the Request:

$request = Request::createFromGlobals();
$session = $request->getSession();

didn’t work. (null object)

currently I am providing my methods with the session which i retrieve in a Controller, but I’m not satisfied with this solution (I think it’s a dirty solution).

How do you do this the right way?

Regards,
Senad

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:47:55+00:00Added an answer on June 4, 2026 at 7:47 am

    You can solve this problem in many ways. One way is to use Dependency Injection which is used throughout the framework.

    If you are passing the session to your extensions constructor, you are “injecting” the session into the class.

    example code

    class MyService
    {
        public function __construct($session)
        {
            // ...
        }
    }
    
    
    class MyController extends Controller
    {
        public function exampleAction()
        {
            $myService = new MyService($this->get('session'));
        }
    }
    

    Symfony 2 provides a way to inject this session automatically. To do this you need to create a service:

    services.yml example code

    services:
        myService:
            class: %myService.class%
            arguments: ["@session"]
    

    Then in your controller you can get the service:

    $myService = $this->get('myService');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I access session object from within a function where request object is not
How can I access a user session objects from another thread? I want to
I want to access a session variable from my layout master page, but you
If I want to set a variable that my whole controller can access, how
i get an error when i try to access HttpContext.Current from a controller. I
I want to get info from Session object and update some info from a
I'd like to get access to the current executing Controller so I can offload
How can I access session variable from the HttpModule ? I set following session
I want to access this page which returns an xml file after entering the
I want to access an online music library from my android application to play

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.