Logo
  • General contributor guidelines
  • Changelog
  • Tutorial
  • Create an app
  • Navigation and pre-app configuration
    • Adding a navigation entry
    • Further pre-app configuration
    • Best practice
      • appinfo/app.php
      • lib/AppInfo/Application.php
  • App metadata
  • Classloader
  • Request lifecycle
  • Routing
  • Middleware
  • Container
  • Controllers
  • RESTful API
  • Templates
  • JavaScript
  • CSS
  • Translation
  • Theming support
  • Database schema
  • Database access
  • Configuration
  • Filesystem
  • AppData
  • User management
  • Two-factor providers
  • Hooks
  • Background jobs (Cron)
  • Settings
  • Logging
  • Migrations
  • Repair steps
  • Testing
  • App store publishing
  • Code signing
  • App development
  • Design guidelines
  • Android application development
  • Client APIs
  • Core development
  • Bugtracker
  • Help and communication
  • API Documentation
Nextcloud 14 Developer Manual
  • »
  • App development »
  • Navigation and pre-app configuration
  • Edit on GitHub

Navigation and pre-app configuration¶

Adding a navigation entry¶

Navigation entries for apps can be created by adding a navigation section to the appinfo/info.xml file, containing the name, order and route the navigation entry should link to. For details on the XML schema check the app store documentation.

<navigation>
    <name>MyApp</name>
    <route>myapp.page.index</route>
    <order>0</order>
</navigation>

Further pre-app configuration¶

The appinfo/app.php is the first file that is loaded and executed in Nextcloud. Depending on the purpose of the app it is usually used to setup things that need to be available on every request to the server, like Background jobs (Cron) and Hooks registrations. This is how an example appinfo/app.php could look like:

<?php

// execute OCA\MyApp\BackgroundJob\Task::run when cron is called
\OC::$server->getJobList()->add('OCA\MyApp\BackgroundJob\Task');

// execute OCA\MyApp\Hooks\User::deleteUser before a user is being deleted
\OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\MyApp\Hooks\User', 'deleteUser');

Although it is also possible to include JavaScript or CSS for other apps by placing the addScript or addStyle functions inside this file, it is strongly discouraged, because the file is loaded on each request (also such requests that do not return HTML, but e.g. json or webdav).

<?php

\OCP\Util::addScript('myapp', 'script');  // include js/script.js for every app
\OCP\Util::addStyle('myapp', 'style');  // include css/style.css for every app

Best practice¶

A common way to have a cleaner code structure is to create a class Application in lib/AppInfo/Application.php that will then execute your setup of hooks or background tasks. You can then just call it in your appinfo/app.php. That way you can also make use of Nextclouds dependency injection feature and properly test those methods.

appinfo/app.php¶

<?php

$app = new \OCA\MyApp\AppInfo\Application();
$app->registerHooks();

lib/AppInfo/Application.php¶

<?php
namespace OCA\MyApp\AppInfo;

use OCP\AppFramework\App;

class Application extends App {

    public function registerHooks() {
        \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\MyApp\Hooks\User', 'deleteUser');
    }

}
Next Previous

© Copyright 2021 Nextcloud GmbH.

Read the Docs v: 14
Versions
14
15
16
stable
latest
Downloads
On Read the Docs
Project Home
Builds