0

I have this folder structure (symfony 2 application):

src
|-- Application
|   |-- UserBundle
|   |   |-- Admin
|   |   |-- Api
|   |       |-- SomeController.php
|   |   |-- Controller
|   |       |-- DefaultController.php
|   |   |-- DependencyInjection
|   |   |-- Entity
|   |   |-- Resources
|   |   |-- Tests

I want to call some controller from Api folder. How i can do this?

UPD.

I use symfony routing to provide the controller:

application_homepage:
    path:     /hello/{name}
    defaults: { _controller: ApplicationUserBundle:Default:index }

application_some:
    path:     /api/{slug}
    defaults: { _controller: ApplicationUserBundle:../Api/Some:index }

I want to load controller from Api folder

1
  • Please provide the code to show what you have attempted Commented Aug 11, 2015 at 7:19

3 Answers 3

1

If the namespace of your controller is : Application\UserBundle\Api, that the className is SomeController and the action is indexAction

You can use this syntax in your routing file :

application_some:
    path:     /api/{slug}
    defaults: { _controller: Application\UserBundle\Api\SomeController::indexAction }
Sign up to request clarification or add additional context in comments.

Comments

1

This solution will work too:

application_some:
    resource: "@ApplicationUserBundle/Api"
    type: annotation

or:

application_some:
    resource: "@ApplicationUserBundle/Api/SomeController.php"
    type: annotation

see http://symfony.com/doc/current/book/routing.html#including-external-routing-resources

Comments

0

If both controller sit within the same namespace, you could simply do

$controller = new ControllerInApiFolder();
$conotroller->someFunction();

if they do not, then you need to include the controller via use-statement.

use Namespace\My\Controller\Sits\In\ControllerInApiFolder;

If you are trying to access the controller not from another controller, but from a template, an url or a testcase, you should refer to the symfony2 documentation.

Symfony2 Doc

From the edited question above I take it you want to call the controller via the url. In your browser you can simply type in

path/to/my/web/api/something

or

path/to/my/web/app_dev.php/api/something

where "path/to/my/web" referes to the path of the web folder within your project

EDIT: I think now I am getting the problem... Symfony2 routing always defaults to the /Controller folder to look for the controller (which is quite nice).

I am not quite sure you should add another folder to this. Instead, the documentation suggests having multiple folders inside the /Controller folder. If you take a look at the folder structure displayed here: Symfony2 Controller, you will notice that there is an API folder within the /Controller folder.

<your-project>/
├─ ...
└─ src/
   └─ AppBundle/
      ├─ ...
      └─ Controller/
         ├─ DefaultController.php
         ├─ ...
         ├─ Api/
         │  ├─ ...
         │  └─ ...
         └─ Backend/
            ├─ ...
            └─ ...

1 Comment

Yes, i want to call the controller via url. But i want to call controller from Api folder, not Controller folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.