mirror of
https://github.com/nextcloud/documentation.git
synced 2026-01-03 02:09:45 +07:00
Merge pull request #9285 from nextcloud/enhancement/deprecate-pascal-case-app-container-parameters
Document deprecation of pascal case DI container parameters
This commit is contained in:
@@ -107,8 +107,8 @@ This route calls the controller **OCA\\notestutorial\\PageController->index()**
|
||||
|
||||
class PageController extends Controller {
|
||||
|
||||
public function __construct(string $AppName, IRequest $request){
|
||||
parent::__construct($AppName, $request);
|
||||
public function __construct(string $appName, IRequest $request){
|
||||
parent::__construct($appName, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,8 +133,8 @@ Since the route which returns the initial HTML has been taken care of, the contr
|
||||
|
||||
class NoteController extends Controller {
|
||||
|
||||
public function __construct(string $AppName, IRequest $request){
|
||||
parent::__construct($AppName, $request);
|
||||
public function __construct(string $appName, IRequest $request){
|
||||
parent::__construct($appName, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -386,7 +386,7 @@ Connect database & controllers
|
||||
|
||||
The mapper which provides the database access is finished and can be passed into the controller.
|
||||
|
||||
You can pass in the mapper by adding it as a type hinted parameter. Nextcloud will figure out how to :doc:`assemble them by itself <../basics/dependency_injection>`. Additionally we want to know the userId of the currently logged in user. Simply add a **$UserId** parameter to the constructor (case sensitive!). To do that open **notestutorial/lib/Controller/NoteController.php** and change it to the following:
|
||||
You can pass in the mapper by adding it as a type hinted parameter. Nextcloud will figure out how to :doc:`assemble them by itself <../basics/dependency_injection>`. Additionally we want to know the userId of the currently logged in user. Simply add a **$userId** parameter to the constructor (case sensitive!). To do that open **notestutorial/lib/Controller/NoteController.php** and change it to the following:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
@@ -408,10 +408,10 @@ You can pass in the mapper by adding it as a type hinted parameter. Nextcloud wi
|
||||
private NoteMapper $mapper;
|
||||
private ?string $userId;
|
||||
|
||||
public function __construct(string $AppName, IRequest $request, NoteMapper $mapper, ?string $UserId = null){
|
||||
parent::__construct($AppName, $request);
|
||||
public function __construct(string $appName, IRequest $request, NoteMapper $mapper, ?string $userId = null){
|
||||
parent::__construct($appName, $request);
|
||||
$this->mapper = $mapper;
|
||||
$this->userId = $UserId;
|
||||
$this->userId = $userId;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -654,11 +654,11 @@ Now we can wire up the trait and the service inside the **NoteController**:
|
||||
|
||||
use Errors;
|
||||
|
||||
public function __construct(string $AppName, IRequest $request,
|
||||
NoteService $service, ?string $UserId = null) {
|
||||
parent::__construct($AppName, $request);
|
||||
public function __construct(string $appName, IRequest $request,
|
||||
NoteService $service, ?string $userId = null) {
|
||||
parent::__construct($appName, $request);
|
||||
$this->service = $service;
|
||||
$this->userId = $UserId;
|
||||
$this->userId = $userId;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -967,11 +967,11 @@ With that in mind create a new controller in **notestutorial/lib/Controller/Note
|
||||
|
||||
use Errors;
|
||||
|
||||
public function __construct($AppName, IRequest $request,
|
||||
NoteService $service, ?string $UserId = null) {
|
||||
parent::__construct($AppName, $request);
|
||||
public function __construct(string $appName, IRequest $request,
|
||||
NoteService $service, ?string $userId = null) {
|
||||
parent::__construct($appName, $request);
|
||||
$this->service = $service;
|
||||
$this->userId = $UserId;
|
||||
$this->userId = $userId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,7 +33,10 @@ tbd
|
||||
Back-end changes
|
||||
----------------
|
||||
|
||||
tbd
|
||||
Dependency Injection Parameters
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
App container parameters with pascal case names ``AppName``, ``UserId`` and ``WebRoot`` are deprecated. Use the camel case variants ``appName``, ``userId`` and ``webRoot`` instead if you have them injected into one of your app classes.
|
||||
|
||||
Changed APIs
|
||||
^^^^^^^^^^^^
|
||||
|
||||
@@ -238,8 +238,8 @@ Then session variables can be accessed like this:
|
||||
|
||||
private ISession $session;
|
||||
|
||||
public function __construct($AppName, IRequest $request, ISession $session) {
|
||||
parent::__construct($AppName, $request);
|
||||
public function __construct($appName, IRequest $request, ISession $session) {
|
||||
parent::__construct($appName, $request);
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ use the **registerService** method on the container object:
|
||||
*/
|
||||
$container->registerService(AuthorController::class, function(ContainerInterface $c): AuthorController {
|
||||
return new AuthorController(
|
||||
$c->get('AppName'),
|
||||
$c->get('appName'),
|
||||
$c->get(Request::class),
|
||||
$c->get(AuthorService::class)
|
||||
);
|
||||
@@ -139,12 +139,12 @@ The container works in the following way:
|
||||
* The matched route queries **AuthorController** service from the container::
|
||||
|
||||
return new AuthorController(
|
||||
$c->get('AppName'),
|
||||
$c->get('appName'),
|
||||
$c->get(Request::class),
|
||||
$c->get(AuthorService::class)
|
||||
);
|
||||
|
||||
* The **AppName** is queried and returned from the base class
|
||||
* The **appName** is queried and returned from the base class
|
||||
* The **Request** is queried and returned from the server container
|
||||
* **AuthorService** is queried::
|
||||
|
||||
@@ -198,9 +198,9 @@ So basically the following is now possible:
|
||||
public MyTestClass $class;
|
||||
public string $appName;
|
||||
|
||||
public function __construct(MyTestClass $class, string $AppName) {
|
||||
public function __construct(MyTestClass $class, string $appName) {
|
||||
$this->class = $class;
|
||||
$this->appName = $AppName;
|
||||
$this->appName = $appName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ So basically the following is now possible:
|
||||
$class2->appName === 'myname'; // true
|
||||
$class2 === $app->getContainer()->get(MyTestClass2::class); // true
|
||||
|
||||
.. note:: $AppName is resolved because the container registered a parameter under the key 'AppName' which will return the app id. The lookup is case sensitive so while $AppName will work correctly, using $appName as a constructor parameter will fail.
|
||||
.. note:: $appName is resolved because the container registered a parameter under the key 'appName' which will return the app id.
|
||||
|
||||
How does it affect the request lifecycle
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@@ -252,8 +252,8 @@ The only thing that needs to be done to add a route and a controller method is n
|
||||
use OCP\IRequest;
|
||||
|
||||
class PageController {
|
||||
public function __construct($AppName, IRequest $request) {
|
||||
parent::__construct($AppName, $request);
|
||||
public function __construct($appName, IRequest $request) {
|
||||
parent::__construct($appName, $request);
|
||||
}
|
||||
|
||||
public function index() {
|
||||
@@ -309,16 +309,17 @@ The following parameter names and type hints can be used to inject core services
|
||||
|
||||
Parameters:
|
||||
|
||||
* **AppName**: The app id
|
||||
* **UserId**: The id of the current user
|
||||
* **WebRoot**: The path to the Nextcloud installation
|
||||
* **appName**: The app id
|
||||
* **userId**: The id of the current user
|
||||
* **webRoot**: The path to the Nextcloud installation
|
||||
|
||||
Aliases:
|
||||
* **appName**: resolves to ``AppName``
|
||||
* **Request**: resolves to ``\OCP\\IRequest``
|
||||
|
||||
* **AppName**: resolves to ``appName`` (deprecated)
|
||||
* **Request**: resolves to ``\OCP\IRequest``
|
||||
* **ServerContainer**: resolves to ``\OCP\IServerContainer``
|
||||
* **userId**: resolves to ``UserId``
|
||||
* **webRoot**: resolves to ``WebRoot``
|
||||
* **UserId**: resolves to ``userId`` (deprecated)
|
||||
* **WebRoot**: resolves to ``webRoot`` (deprecated)
|
||||
|
||||
Types:
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ The config that allows the app to set global, app and user settings can be injec
|
||||
$container->registerService('AuthorService', function(IServerContainer $c): AuthorService {
|
||||
return new AuthorService(
|
||||
$c->get(IConfig::class),
|
||||
$c->get('AppName')
|
||||
$c->get('appName')
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user