Develop a simple package in concrete.
So now we will start work on a package.
1) Create the package directory
First of all, create a directory in the package directory of your application. Your directory name should be whatever your package name is. I will use the directory name sample_package. So now you have the directory structure Concrete/Package/sample_package. Here, concrete is my application name.
2) Create the controller
Create a file in Concrete/Package/sample_package named controller.php. This file will be responsible for installation, uninstall, upgrade, and other functionalities.
Here is sample code:
<?php
namespace Concrete\Package\SamplePackage;
use Package;
/**
* This is the main controller for the package which controls the functionality like Install/Uninstall etc.
*
* @author AN 05/16/2016
*/
class Controller extends Package
{
/**
* Protected data members for controlling the instance of the package
*/
protected $pkgHandle = 'sample_package';
protected $appVersionRequired = '5.7.5.6';
protected $pkgVersion = '0.0.1';
/**
* This function returns the functionality description ofthe package.
*
* @param void
* @return string $description
* @author AN 05/16/2016
*/
public function getPackageDescription()
{
return t("A package to demo of package development.");
}
/**
* This function returns the name of the package.
*
* @param void
* @return string $name
* @author AN 05/16/2016
*/
public function getPackageName()
{
return t("sample package");
}
/**
* This function is executed during initial installation of the package.
*
* @param void
* @return void
* @author AN 05/16/2016
*/
public function install()
{
$pkg = parent::install();
// Install Single Pages
$this->install_single_pages($pkg);
}
/**
* This function is executed during uninstallation of the package.
*
* @param void
* @return void
* @author AN 05/16/2016
*/
public function uninstall()
{
$pkg = parent::uninstall();
}
/**
* This function is used to install single pages.
*
* @param type $pkg
* @return void
* @author AN 05/16/2016
*/
function install_single_pages($pkg)
{
$directoryDefault = SinglePage::add('/dashboard/sample_package/', $pkg);
$directoryDefault->update(array('cName' => t('Sample Package'), 'cDescription' => t('Sample Package')));
}
}
So you can see there are some basic functions defined in this controller. Don't be confused to see install_single_pages functionality. I will describe it later. So copy this code into your controller.php and save it.
3) Give an icon to your package
If you want (and it's recommended), give your package an icon. It is so easy: Put a 97px X 97px .png image in your package folder (Concrete/Package/sample_package). You must name this image icon.png.
4) Create a directory structure for the controller
Create this directory structure for your controller file:
Concrete/Package/sample_package/controller/single_page/dashboard
Then create a file with the name sample_package.php in the dashboard directory and put this code:
<?php
namespace Concrete\Package\SamplePackage\Controller\SinglePage\Dashboard;
use \Concrete\Core\Page\Controller\DashboardPageController;
defined('C5_EXECUTE') or die(_("Access Denied."));
/**
* This is the controller for the package which controls the functionality for /dashboard/database/ route.
*
* @author AN 05/16/2016
*/
class SamplePackage extends DashboardPageController
{
/**
* Function to set the variables for view.
*
* @param void
* @author AN 05/16/2016
*/
public function view()
{
// Here you can set variable for view $this->set('nameForView',$variable);
}
}
5) Create a directory structure for single pages
Create this directory structure for the controller file: Concrete/Package/sample_package/single_page/dashboard/sample_package
Next, create a file with name view.php in the dashboard directory and put any html code.
Example:
Hi Sample package
Now install the package and you will get a menu option "sample package". Click it or add this url after index: /dashboard/sample_package. Your page will be shown. You can customize this according your need.
Let me know if this post need any improvement and have any error.
Thanks.