Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a custom class in a Symfony Bundle?

Tags:

php

class

symfony

I have a Symfony bundle where I will have to use a custom class. This class does not have to be accessible from all the website, but just in a controller of this bundle. I have seen a few solutions relative to the vendors, but this is quite heavy and not necessary in my case. Does someone have a simpler solution?

like image 978
maxime Avatar asked Jul 03 '13 14:07

maxime


1 Answers

This is what namespaces are for.

From php.net:

What are namespaces? In the broadest definition namespaces are a way of encapsulating items.

Simply put, include your namespace at the top of your custom class.

src/Acme/DemoBundle/Model/MyClass.php

<?php 
namespace Acme\DemoBundle\Model;

class MyClass { // ...

and use it in your Controller:

src/Acme/SomeOtherBundle/Controller/DefaultController.php

<?php
namespace Acme\SomeOtherBundle\Controller;

// ...
use Acme\DemoBundle\Model\MyClass; # can be used in any class in any bundle
// ...

class DefaultController extends Controller { // ...
like image 90
Carrie Kendall Avatar answered Sep 25 '22 20:09

Carrie Kendall