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 7441597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:02:29+00:00 2026-05-29T11:02:29+00:00

In Symfony2, a service is defined as [emphasis mine]: A Service is a generic

  • 0

In Symfony2, a service is defined as [emphasis mine]:

A Service is a generic term for any PHP object that performs a
specific task. A service is usually used “globally“, such as a
database connection object or an object that delivers email messages.
In Symfony2, services are often configured and retrieved from the
service container. An application that has many decoupled services is
said to follow a service-oriented architecture.

With “globally” being the key word, all the examples I see of how to define a service have the service declared within an existing bundle? Here’s an example from MartinSikora.com

<?php
    // Bundle/HelloBundle/Services/MyService.php
    namespace Bundle\HelloBundle\Services;

    class MyService {

        public function sum($n1, $n2) {
            return $n1 + $n2;
        }

    }
?>

He then uses it from within the Hello Controller:

<?php
// Bundle/HelloBundle/Controller/HelloController.php
namespace Bundle\HelloBundle\Controller;

class HelloController extends Controller {

    public function indexAction() {
        $number = $this->get('my_service')->sum(12, 37);
        // this returns 49

        /*
        ...
        */
    }
}
?>

Notice how his example service is declared inside of the HelloBundle bundle in a folder labeled “Services”. Wouldn’t it be better if the “Services” folder were stored one or more levels higher outside of any specific bundle since services are meant to be used across the entire application?

  • What’s the best practice here?
  • Is the service only available to controllers within the HelloBundle Bundle?
  • Why is it usually done this way?
  • 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-05-29T11:02:30+00:00Added an answer on May 29, 2026 at 11:02 am

    Short answer
    No, services do not need to be defined within a bundle. The actual service class may or may not live within a bundle – they may live in a vendor library folder (or anywhere else for that matter). And service definitions, while commonly defined within a bundle’s Resources\config\services.yml, can also be defined in app/config.yml under a services key.

    A service is simply a class that has been registered with Symfony’s dependency injection container. The class itself can live anywhere.

    Long answer
    It’s important to understand the purpose of bundles in Symfony2. The majority of your project (with the main exception being 3rd party libraries) is comprised of bundles. Some people like to put everything in one giant bundle, but I prefer to use them as containers for specific pieces of functionality (ie: User management, Blog Post management, Asset management.)

    Because bundles should represent a piece of functionality, it makes sense to define some services within bundles. For example, it’s natural to define a BlogPostEntityService class in a BlogPostBundle. Now, just because the service is contained within a bundle, doesn’t make it any less global. If I register my service as blog_bundle.blog_post_entity_service, I can still access it from any other bundle.

    In Martin’s example, MyService is a vague example – it isn’t specific like BlogPostEntityService. However, it is likely that you’ll create utility services (ie ArrayUtilService), in which case you might want to create a UtilBundle and store the service there.

    What’s the best practice here?

    There’s no definitive answer – it’s up to you to decide where to store service classes. Ask yourself if the service deals directly with the piece of functionality your bundle represents – if so, it likely belongs in the bundle. This also makes sense if you plan on sharing or reusing your bundle.

    Is the service only available to controllers within the HelloBundle
    Bundle?

    No. You can call the service from within any controller (or any container aware class). You can also inject MyService into any other service.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a functional test for an action that uses Symfony2's session service to
I'm learning Symfony2 (and OOP) and want to create a service that's available throughout
I want to create a service in Symfony2 that will take in the current
In symfony2 project php bin/vendors install , re-installs all bundles in deps file. Each
In Symfony2 (2.0.3) I have a BetaBundle that is set as the parent of
I'm coming from Symfony2's world (PHP) and I'm trying to find a decent equivalent
I am running symfony2 in Windows7 and Xampp: http://localhost/symfony/web/app_dev.php/hello/symfony How do I change web/.htaccess
Just a quick question, wondering if objects retrieved from the service container in Symfony2
I have a class that I want symfony2 to autoload so I can reference
I have a Service (DependencyInjection) that i create, and i use that on my

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.