mirror of
https://github.com/nextcloud/documentation.git
synced 2026-01-02 17:59:36 +07:00
Merge pull request #1091 from owncloud/usesession
Document usesession annotation
This commit is contained in:
@@ -209,9 +209,9 @@ It is possible to pass JSON using a POST, PUT or PATCH request. To do that the *
|
||||
|
||||
}
|
||||
|
||||
Headers, files, cookies and environment variables
|
||||
-------------------------------------------------
|
||||
Headers, files, cookies and environment variables can be accessed directly from the request object. Every controller depends on the app name and the request object and sets both on protected attributes:
|
||||
Reading headers, files, cookies and environment variables
|
||||
---------------------------------------------------------
|
||||
Headers, files, cookies and environment variables can be accessed directly from the request object:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
@@ -223,25 +223,99 @@ Headers, files, cookies and environment variables can be accessed directly from
|
||||
|
||||
class PageController extends Controller {
|
||||
|
||||
public function __construct($appName, IRequest $request) {
|
||||
parent::__construct($appName, $request);
|
||||
}
|
||||
|
||||
|
||||
public function someMethod() {
|
||||
|
||||
$type = $this->request->getHeader('Content-Type'); // $_SERVER['HTTP_CONTENT_TYPE']
|
||||
$cookie = $this->request->getCookie('myCookie'); // $_COOKIES['myCookie']
|
||||
$file = $this->request->getUploadedFile('myfile'); // $_FILES['myfile']
|
||||
$env = $this->request->getEnv('SOME_VAR'); // $_ENV['SOME_VAR']
|
||||
|
||||
// access the app name
|
||||
$name = $this->appName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Why should those values be accessed from the request object and not from the global array like $_FILES? Simple: `because it's bad practice <http://c2.com/cgi/wiki?GlobalVariablesAreBad>`_
|
||||
Why should those values be accessed from the request object and not from the global array like $_FILES? Simple: `because it's bad practice <http://c2.com/cgi/wiki?GlobalVariablesAreBad>`_ and will make testing harder.
|
||||
|
||||
|
||||
Reading and writing session variables
|
||||
-------------------------------------
|
||||
To set, get or modify session variables, the ISession object has to be injected into the controller.
|
||||
|
||||
Then session variables can be accessed like this:
|
||||
|
||||
.. note:: The session is closed automatically for writing, unless you add the @UseSession annotation!
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
namespace OCA\MyApp\Controller;
|
||||
|
||||
use OCP\ISession;
|
||||
use OCP\IRequest;
|
||||
use OCP\AppFramework\Controller;
|
||||
|
||||
class PageController extends Controller {
|
||||
|
||||
private $session;
|
||||
|
||||
public function __construct($AppName, IRequest $request, ISession $session) {
|
||||
parent::__construct($AppName, $request);
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
/**
|
||||
* The following annotation is only needed for writing session values
|
||||
* @UseSession
|
||||
*/
|
||||
public function writeASessionVariable() {
|
||||
// read a session variable
|
||||
$value = $this->session['value'];
|
||||
|
||||
// write a session variable
|
||||
$this->session['value'] = 'new value';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Setting cookies
|
||||
---------------
|
||||
Cookies can be set or modified directly on the response class:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
namespace OCA\MyApp\Controller;
|
||||
|
||||
use DateTime;
|
||||
|
||||
use OCP\AppFramework\Controller;
|
||||
use OCP\AppFramework\Http\TemplateResponse;
|
||||
use OCP\IRequest;
|
||||
|
||||
class BakeryController extends Controller {
|
||||
|
||||
/**
|
||||
* Adds a cookie "foo" with value "bar" that expires after user closes the browser
|
||||
* Adds a cookie "bar" with value "foo" that expires 2015-01-01
|
||||
*/
|
||||
public function addCookie() {
|
||||
$response = new TemplateResponse(...);
|
||||
$response->addCookie('foo', 'bar');
|
||||
$response->addCookie('bar', 'foo', new DateTime('2015-01-01 00:00'));
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates the cookie "foo"
|
||||
* Invalidates the cookie "bar" and "bazinga"
|
||||
*/
|
||||
public function invalidateCookie() {
|
||||
$response = new TemplateResponse(...);
|
||||
$response->invalidateCookie('foo');
|
||||
$response->invalidateCookies(array('bar', 'bazinga'));
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Responses
|
||||
=========
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
=================================
|
||||
User, Session & Cookie Management
|
||||
=================================
|
||||
==============
|
||||
Usermanagement
|
||||
==============
|
||||
|
||||
.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>
|
||||
|
||||
@@ -171,124 +171,3 @@ Then users can be logged in by using:
|
||||
|
||||
}
|
||||
|
||||
|
||||
Session Information
|
||||
===================
|
||||
To set, get or modify session variables, the Session has to be injected from the ServerContainer:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
namespace OCA\MyApp\AppInfo;
|
||||
|
||||
use \OCP\AppFramework\App;
|
||||
|
||||
use \OCA\MyApp\Service\SessionService;
|
||||
|
||||
|
||||
class Application extends App {
|
||||
|
||||
public function __construct(array $urlParams=array()){
|
||||
parent::__construct('myapp', $urlParams);
|
||||
|
||||
$container = $this->getContainer();
|
||||
|
||||
/**
|
||||
* Controllers
|
||||
*/
|
||||
$container->registerService('SessionService', function($c) {
|
||||
return new SessionService(
|
||||
$c->query('Session'),
|
||||
$c->query('TimeFactory')
|
||||
);
|
||||
});
|
||||
|
||||
$container->registerService('Session', function($c) {
|
||||
return $c->query('ServerContainer')->getSession();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Then session variables can be accessed like this:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
namespace OCA\MyApp\Service;
|
||||
|
||||
use \OCP\ISession
|
||||
|
||||
class SessionService {
|
||||
|
||||
private $session;
|
||||
private $timeFactory;
|
||||
|
||||
public function __construct(ISession $session, $timeFactory){
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
public function updateTimestamp() {
|
||||
$oldTime = 0;
|
||||
|
||||
if (array_key_exists('timestamp', $this->session) {
|
||||
$oldTime = $this->session['timestamp'];
|
||||
}
|
||||
|
||||
$newTime = $this->timeFactory->getTime();
|
||||
if ($newTime > $oldTime) {
|
||||
$this->session['timestamp'] = $newTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Managing cookies
|
||||
================
|
||||
To set, get or modify cookies the `request` variable and the `response` class can be used from within controllers:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
namespace OCA\MyApp\Controller;
|
||||
use \OCP\AppFramework\Controller;
|
||||
use \OCP\IRequest;
|
||||
|
||||
class BakeryController extends Controller {
|
||||
|
||||
public function __construct($appName, IRequest $request) {
|
||||
parent::__construct($appName, $request);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the value of the cookie "bar"
|
||||
*/
|
||||
public function getCookie() {
|
||||
$cookie = $this->request->getCookie('bar');
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a cookie "foo" with value "bar" that expires after user closes the browser
|
||||
* Adds a cookie "bar" with value "foo" that expires 2015-01-01
|
||||
*/
|
||||
public function addCookie() {
|
||||
$response = new TemplateResponse(...);
|
||||
$response->addCookie('foo', 'bar');
|
||||
$response->addCookie('bar', 'foo', new \DateTime('2015-01-01 00:00'));
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates the cookie "foo"
|
||||
* Invalidates the cookie "bar" and "bazinga"
|
||||
*/
|
||||
public function invalidateCookie() {
|
||||
$response = new TemplateResponse(...);
|
||||
$response->invalidateCookie('foo');
|
||||
$response->invalidateCookies(array('bar', 'bazinga'));
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user