Domanda

I'm relatively new to Symfony2, so I'm learning by doing. My controller classes are getting pretty big. I'd like to break it up with functions() or objects->method(). Unfortunately I can't figure out where to put the code. (actually its really simple functions... but I can wrap that in an object...)

--I can't add it to the bottom of my DefaultController.php file. It errors out, and not pretty code to boot, either inside or outside of the { }.

--I can't simply add a new NewObject.php file to the controller directory. That errors out. The error: FatalErrorException: ...NewObject not found.

--I've toyed with manual mods to ../app/autoload.php but that doesn't really make sense for a simple class add to my ./SRC/ bundle. Perhaps I should build a ./src/autoload.php file (similiar to ./vender/autoload.php) but the contents of that file don't make sense to me at all. I simply can't figure out how the AnnotationRegistry Loader works.

Am I missing something? This seems way too hard.. what I want is a wrapped up 'include' so I can use the class in dev and after deployment.

How do I include NewObject.php (and the accompanying $newObject->function() ) in my code?

I'm told I can add a service, yet that seems like outrageous overhead for such a seemingly simple task (again, all I'm trying to do is clean up my very long controller php code...)

thanks in advance for your advice.

È stato utile?

Soluzione

So you've got a project structure that looks something like this, right?

project
-- app
-- bin
-- src
   -- SomeName
      -- SomeBundle
          -- Controller
          -- Entity
          -- Resources
          -- ...
-- vendor
-- web

And you're just looking to have some kind of "helper" class that's used throughout your bundle. Is that correct?

If so, then you can really put it wherever you want to inside your src/ directory... Just make sure that the class name matches the file name, and that the path to the file matches the namespace you define at the top of your PHP code.

Sometimes when I do this, I'll create a simple directory under my bundle called "Helper/". Other times, when the application is more complex, I might be a little bit more explicit. But here's what the first case would look like...

First, add your /Helper directory under your bundle, and create the class file:

project
-- app
-- bin
-- src
   -- SomeName
      -- SomeBundle
          -- Controller
          -- Entity
          -- Helper
             -- SomeHelper.php
          -- Resources
          -- ...
-- vendor
-- web

The contents of SomeHelper.php might look like this:

<?php
namespace SomeName\SomeBundle\Helper;

class SomeHelper
{
    public function doSomething()
    {
        ...
    }
}

Because your namespace matches the file path, it gets autoloaded, so you don't need to worry about include statements. You can instantiate that class anywhere in your bundle, as long as you include a use statement:

<?php
namespace SomeName\SomeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use SomeName\SomeBundle\Helper\SomeHelper;

class DefaultController extends Controller
{
    public function indexAction()
    {
        ...
        $helper = new SomeHelper();
        $helper->doSomething();
        ...
    }
}

Regarding the usage of services... Yes, that might be overkill, depending on what you're using it for. It's helpful to create services when the class needs to be aware of the application around it. For example, if you're creating a service that emails a User, it might want to access your database through the Doctrine service, or it might want to log the email activity through the Monolog service.

However, if your class doesn't need to know about the application (referred to as the "service container"), for example if it's just used to transfer data, then a helper class is probably more appropriate.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top