user3890355
user3890355

Reputation: 1044

How to call Controller from another folder via url Symfony 2?

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

Upvotes: 0

Views: 1270

Answers (3)

user3890355
user3890355

Reputation: 1044

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

Upvotes: 1

Micha
Micha

Reputation: 563

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/
            ├─ ...
            └─ ...

Upvotes: 0

jngermon
jngermon

Reputation: 26

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 }

Upvotes: 1

Related Questions