Visit the official documentation of CodeIgniter for more details. In the examples below, the plugin folder is My_Plugin.
Routes
Add the routes of your plugin in My_Plugin/Config/Routes.php, so the main app can find your pages.
<?php
namespace Config;
$routes = Services::routes();
$routes->get('my_plugin', 'My_Plugin::index', ['namespace' => 'My_Plugin\Controllers']);
$routes->get('my_plugin/(:any)', 'My_Plugin::$1', ['namespace' => 'My_Plugin\Controllers']);
$routes->post('my_plugin/(:any)', 'My_Plugin::$1', ['namespace' => 'My_Plugin\Controllers']); //needed if any data is posted
Controllers
<?php
namespace My_Plugin\Controllers;
use App\Controllers\Security_Controller; //access main app's controller
class My_Plugin extends Security_Controller {
//your methods
}
Use Security_Controller for pages that need a signed in user. Use App_Controller for public pages.
Models
<?php
namespace My_Plugin\Models;
use App\Models\Crud_model; //access main app's models
class My_Plugin_Model extends Crud_model {
//your methods
}
Calling the model:
$My_Plugin_Model = new \My_Plugin\Models\My_Plugin_Model();
Libraries
<?php
namespace My_Plugin\Libraries;
class My_Plugin_Library {
//your methods
}
Calling the library:
$my_plugin_library = new \My_Plugin\Libraries\My_Plugin_Library();
Languages
Use the same folder design as the main app, like My_Plugin/Language/english/default_lang.php and custom_lang.php. Add a unique prefix to your language keys. For example:
<?php $lang["my_plugin_show_staff_members"] = "Show staff members";
Then use app_lang("my_plugin_show_staff_members"); to get the text. Add a folder for every language you want to support.
Views
$view_data['foo'] = "bar";
//$this->template->rander() shows the view with the left menu and topbar.
return $this->template->rander('My_Plugin\Views\folder\index', $view_data);
//$this->template->view() shows only the view. You get the signed in user's info as $login_user.
return $this->template->view('My_Plugin\Views\folder\index', $view_data);
//default view() function of CodeIgniter
return view('My_Plugin\Views\folder\index', $view_data);
Helpers
Add a helper file in the My_Plugin/Helpers folder (like my_plugin_general_helper.php) and load it like this:
helper("my_plugin_general_helper");
Third party libraries
require_once(PLUGINPATH . "My_Plugin/ThirdParty/library_folder/vendor/autoload.php");
Loading assets
<link rel='stylesheet' type='text/css' href='" . base_url(PLUGIN_URL_PATH . "My_Plugin/assets/css/my_plugin_styles.css") . "' />
File management
Use the same functions as the main app for uploads and files. Only change the target folder to your plugin's folder:
PLUGIN_URL_PATH . "My_Plugin/files/my_plugin_files/"
From RISE 4.2, please use the common file functions. Then your files also work with Google Drive and storage plugins. See the article Work with files from a plugin.