mirror of
https://github.com/nextcloud/documentation.git
synced 2025-10-26 11:18:02 +00:00
There is no need to register controllers. DI all the way
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
parent
c2f3b34579
commit
8a7681dc51
@ -2,6 +2,7 @@
|
||||
Classloader
|
||||
===========
|
||||
|
||||
.. app_classloader:
|
||||
.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>
|
||||
|
||||
The classloader is provided by Nextcloud and loads all your classes automatically. The only thing left to include by yourself are 3rdparty libraries. Those should be loaded in :file:`lib/AppInfo/Application.php`.
|
||||
|
||||
@ -28,40 +28,10 @@ To create a controller, simply extend the Controller class and create a method t
|
||||
Connecting a controller and a route
|
||||
-----------------------------------
|
||||
|
||||
To connect a controller and a route the controller has to be registered in the :doc:`container` like this:
|
||||
If you use a proper namespace for your app (see :ref:`app_classloader`) Nextcloud
|
||||
will resolve your controller and its dependencies automatically.
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
namespace OCA\MyApp\AppInfo;
|
||||
|
||||
use OCP\AppFramework\App;
|
||||
|
||||
use OCA\MyApp\Controller\AuthorApiController;
|
||||
|
||||
|
||||
class Application extends App {
|
||||
|
||||
public function __construct(array $urlParams=array()){
|
||||
parent::__construct('myapp', $urlParams);
|
||||
|
||||
$container = $this->getContainer();
|
||||
|
||||
/**
|
||||
* Controllers
|
||||
*/
|
||||
$container->registerService('AuthorApiController', function($c) {
|
||||
return new AuthorApiController(
|
||||
$c->query('AppName'),
|
||||
$c->query('Request')
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Every controller needs the app name and the request object passed into their parent constructor, which can easily be injected like shown in the example code above. The important part is not the class name but rather the string which is passed in as the first parameter of the **registerService** method.
|
||||
|
||||
The other part is the route name. An example route name would look like this::
|
||||
An example route name would look like this::
|
||||
|
||||
author_api#some_method
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user