mirror of
https://github.com/nextcloud/documentation.git
synced 2026-01-03 18:26:42 +07:00
finished the tutorial
This commit is contained in:
@@ -3,28 +3,42 @@ App Tutorial
|
||||
|
||||
.. sectionauthor:: Bernhard Posselt <nukeawhale@gmail.com>
|
||||
|
||||
This tutorial contains the traditional approach to write an app and continues where :doc:`../intro/createapp` left off.
|
||||
This tutorial contains the traditional approach to write an app and continues where :doc:`../intro/createapp` left off. The result will be a simple "Hello World" app.
|
||||
|
||||
Navigation entry
|
||||
~~~~~~~~~~~~~~~~
|
||||
To add a navigation entry
|
||||
----------------
|
||||
This file will always loaded for every app and can for instance be used to load additional JavaScript for the files app. Therefore the navigation entry has to be registered in this file.
|
||||
|
||||
:file:`appinfo/app.php`
|
||||
:file:`appinfo/app.php`:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
|
||||
\OCP\App::addNavigationEntry(array(
|
||||
|
||||
// the string under which your app will be referenced in owncloud
|
||||
'id' => 'myapp',
|
||||
|
||||
// sorting weight for the navigation. The higher the number, the higher
|
||||
// will it be listed in the navigation
|
||||
'order' => 74,
|
||||
|
||||
// the route that will be shown on startup
|
||||
'href' => \OCP\Util::linkToRoute('myapp_index'),
|
||||
|
||||
// the icon that will be shown in the navigation
|
||||
// this file needs to exist in img/example.png
|
||||
'icon' => \OCP\Util::imagePath('myapp', 'nav-icon.png'),
|
||||
|
||||
// the title of your application. This will be used in the
|
||||
// navigation or on the settings page of your app
|
||||
'name' => 'My App'
|
||||
));
|
||||
|
||||
Create the main route
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
---------------------
|
||||
:doc:`routes` map the URL to functions and allow to extract values. To show the content when the navigation entry is clicked, the index route which was defined in the :file:`appinfo/app.php` needs to be created:
|
||||
|
||||
:file:`appinfo/routes.php`:
|
||||
|
||||
@@ -40,7 +54,8 @@ Create the main route
|
||||
|
||||
|
||||
Write the logic
|
||||
~~~~~~~~~~~~~~~
|
||||
---------------
|
||||
In this example the logic is written procedurally in a PHP file. This file contains database queries and security checks and prints the final template:
|
||||
|
||||
:file:`index.php`:
|
||||
|
||||
@@ -57,10 +72,9 @@ Write the logic
|
||||
$tpl->printPage();
|
||||
|
||||
|
||||
|
||||
|
||||
Write the HTML in the template
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Create the template
|
||||
-------------------
|
||||
The last thing that needs to be done is to create the :doc:`templates` file which was used in the :file:`index.php`.
|
||||
|
||||
:file:`templates/main.php`:
|
||||
|
||||
@@ -69,4 +83,4 @@ Write the HTML in the template
|
||||
<p><?php p($_['msg']); ?></p>
|
||||
|
||||
|
||||
|
||||
Congratulations! The message "Hello World" can now be seen on the main page of your app.
|
||||
@@ -3,11 +3,11 @@ App Tutorial
|
||||
|
||||
.. sectionauthor:: Bernhard Posselt <nukeawhale@gmail.com>
|
||||
|
||||
This tutorial contains the MVC approach to write an app. The goal of this tutorial is a simple notes app.
|
||||
This tutorial contains the MVC approach to write an app and continues where :doc:`../intro/createapp` left off. The result will be a simple "Hello World" app.
|
||||
|
||||
Create an app entry
|
||||
-------------------
|
||||
Depending on the . This file will always loaded for every app and can for instance be used to load additional JavaScript for the files app:
|
||||
Create an navigation entry
|
||||
--------------------------
|
||||
The **app.php** will always loaded for every app and can for instance be used to load additional JavaScript for the files app. Therefore the navigation entry has to be registered in this file.
|
||||
|
||||
:file:`appinfo/app.php`
|
||||
|
||||
@@ -15,9 +15,9 @@ Depending on the . This file will always loaded for every app and can for instan
|
||||
|
||||
<?php
|
||||
|
||||
namespace OCA\MyNotes;
|
||||
namespace OCA\MyApp;
|
||||
|
||||
$api = new \OCA\AppFramework\Core\API('mynotes');
|
||||
$api = new \OCA\AppFramework\Core\API('myapp');
|
||||
|
||||
$api->addNavigationEntry(array(
|
||||
|
||||
@@ -29,10 +29,11 @@ Depending on the . This file will always loaded for every app and can for instan
|
||||
'order' => 10,
|
||||
|
||||
// the route that will be shown on startup
|
||||
'href' => $api->linkToRoute('mynotes_index'),
|
||||
'href' => $api->linkToRoute('myapp_index'),
|
||||
|
||||
// the icon that will be shown in the navigation
|
||||
'icon' => $api->imagePath('example.png' ),
|
||||
// this file needs to exist in img/example.png
|
||||
'icon' => $api->imagePath('example.png'),
|
||||
|
||||
// the title of your application. This will be used in the
|
||||
// navigation or on the settings page of your app
|
||||
@@ -43,7 +44,6 @@ Depending on the . This file will always loaded for every app and can for instan
|
||||
|
||||
First Page
|
||||
----------
|
||||
|
||||
Now that the basic files are created, the following things are needed to create a page:
|
||||
|
||||
* **A route**: The URL which links to the controller
|
||||
@@ -51,7 +51,10 @@ Now that the basic files are created, the following things are needed to create
|
||||
* **An entry in the DIContainer**: This makes the controller available for the application
|
||||
* **A template**: HTML which should be displayed on the page
|
||||
|
||||
First the route which is linked in the :file:`appinfo/app.php` needs to be created. To do that create the :doc:`routes` file:
|
||||
|
||||
Create the main route
|
||||
---------------------
|
||||
:doc:`routes` map the URL to functions and allow to extract values. To show the content when the navigation entry is clicked, the index route which was defined in the :file:`appinfo/app.php` needs to be created:
|
||||
|
||||
:file:`appinfo/routes.php`
|
||||
|
||||
@@ -59,18 +62,20 @@ First the route which is linked in the :file:`appinfo/app.php` needs to be creat
|
||||
|
||||
<?php
|
||||
|
||||
namespace OCA\MyNotes;
|
||||
namespace OCA\MyApp;
|
||||
|
||||
use \OCA\AppFramework\App;
|
||||
use \OCA\MyNotes\DependencyInjection\DIContainer;
|
||||
use \OCA\MyApp\DependencyInjection\DIContainer;
|
||||
|
||||
$this->create('mynotes_index', '/')->action(
|
||||
$this->create('myapp_index', '/')->action(
|
||||
function($params){
|
||||
// call the index method on the class PageController
|
||||
App::main('PageController', 'index', $params, new DIContainer());
|
||||
}
|
||||
);
|
||||
|
||||
Write the logic (Controller)
|
||||
----------------------------
|
||||
The :doc:`controllers` to which the route links does not exist yet and it has to be created:
|
||||
|
||||
:file:`controllers/pagecontroller.php`
|
||||
@@ -79,7 +84,7 @@ The :doc:`controllers` to which the route links does not exist yet and it has to
|
||||
|
||||
<?php
|
||||
|
||||
namespace OCA\MyNotes\Controller;
|
||||
namespace OCA\MyApp\Controller;
|
||||
|
||||
use OCA\AppFramework\Controller\Controller;
|
||||
|
||||
@@ -93,28 +98,37 @@ The :doc:`controllers` to which the route links does not exist yet and it has to
|
||||
|
||||
|
||||
/**
|
||||
* ATTENTION!!!
|
||||
* The following comments turn off security checks
|
||||
* Please look up their meaning in the documentation!
|
||||
*
|
||||
* @CSRFExemption
|
||||
* @IsAdminExemption
|
||||
* @IsSubAdminExemption
|
||||
*/
|
||||
public function index(){
|
||||
return $this->render('main');
|
||||
return $this->render('main', array(
|
||||
'msg' => 'Hello World'
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Now create the template:
|
||||
Create the template
|
||||
-------------------
|
||||
Now create the :doc:`templates` which contains the HTML
|
||||
|
||||
:file:`templates/main.php`
|
||||
|
||||
.. code-block:: html
|
||||
|
||||
<div>Hello World</div>
|
||||
<div>{{ msg }}</div>
|
||||
|
||||
|
||||
The last thing that is left is to tell the application how the controller needs to be created. The App Framework makes heavy use of :doc:`../general/dependencyinjection` and provides an IOC Container. Inside this container, the controller needs to be created:
|
||||
Wire everything together
|
||||
------------------------
|
||||
The last thing that is left is to tell the application how the controller needs to be created. The App Framework makes heavy use of :doc:`../general/dependencyinjection` and provides an :doc:`IOC Container <container>`. Inside this container, the controller needs to be created:
|
||||
|
||||
:file:`dependencyinjection/dicontainer.php`
|
||||
|
||||
@@ -125,7 +139,7 @@ The last thing that is left is to tell the application how the controller needs
|
||||
class DIContainer extends BaseContainer {
|
||||
|
||||
public function __construct(){
|
||||
parent::__construct('mynotes');
|
||||
parent::__construct('myapp');
|
||||
|
||||
// use this to specify the template directory
|
||||
$this['TwigTemplateDirectory'] = __DIR__ . '/../templates';
|
||||
@@ -137,4 +151,4 @@ The last thing that is left is to tell the application how the controller needs
|
||||
|
||||
}
|
||||
|
||||
.. todo:: Docs on templates and further stuff
|
||||
Congratulations! The message "Hello World" can now be seen on the main page of your app.
|
||||
Reference in New Issue
Block a user