-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,393 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
--- | ||
sidebar_position: 2 | ||
title: Create a Joomla Library | ||
--- | ||
|
||
Creating a Joomla Library | ||
====================================== | ||
|
||
A Joomla library is a reusable set of classes or functions that can be shared across multiple extensions. Libraries help in organizing and reusing code, making development more efficient and maintainable. | ||
|
||
In this guide, we will create a Joomla library and learn how to use it in Joomla projects. | ||
|
||
## Folder Structure | ||
|
||
The folder structure for a Joomla library is as follows: | ||
|
||
``` | ||
libraries/ | ||
yourlibraryname/ | ||
src/ | ||
YourLibrary.php | ||
yourlibraryname.xml | ||
``` | ||
|
||
### Key Sections of the Library | ||
|
||
- `src/YourLibrary.php`: This is the main file of the library. It contains the class definition and the functions that the library will provide. | ||
- `yourlibraryname.xml`: This file contains the metadata for the library. | ||
|
||
### Extension manifest file | ||
|
||
```xml title="libraries/yourlibraryname/yourlibraryname.xml" | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<extension type="library" method="upgrade"> | ||
<name>Joomla 5 Example Library</name> | ||
<libraryname>JoomlaExampleLibrary</libraryname> | ||
<author>Author Name</author> | ||
<creationDate>2024-09-08</creationDate> | ||
<license>GNU General Public License version 2 or later</license> | ||
<version>1.0.0</version> | ||
<description>Showcase on how to create a Joomla 5 library to reuse code.</description> | ||
<namespace path="src">VendorName\LibraryName</namespace> | ||
<files> | ||
<folder>src</folder> | ||
</files> | ||
</extension> | ||
``` | ||
|
||
### Library Class | ||
|
||
Inside this class is where you will define the functions that the library will provide. Here is an example of a simple library class: | ||
|
||
```php title="libraries/yourlibraryname/src/YourLibrary.php" | ||
<?php | ||
namespace VendorName\LibraryName; | ||
|
||
class YourLibrary | ||
{ | ||
public static function helloWorld() | ||
{ | ||
return "Hello, World!"; | ||
} | ||
|
||
public function greet($name) | ||
{ | ||
return "Hello, " . $name . "!"; | ||
} | ||
|
||
} | ||
``` | ||
|
||
### Packaging the Library for Installation | ||
|
||
Once you have created the library, you need to package it for installation. You can do this by creating a zip file with the following structure: | ||
|
||
``` | ||
yourlibraryname.zip | ||
├── yourlibraryname.xml | ||
└── src/ | ||
└── YourLibrary.php | ||
``` | ||
|
||
With the zip file created, you can install the library using the Joomla extension manager in the Administrator backend. | ||
|
||
## Installation | ||
|
||
1. Go to the Joomla Administrator backend. | ||
2. Go to Extensions > Manage > Install. | ||
3. Upload the zip file containing the library. | ||
|
||
## Usage | ||
|
||
To use the library in your Joomla extension, you need to include the library in your code. Here is an example of how to use the library: | ||
|
||
```php | ||
<?php | ||
use VendorName\LibraryName\YourLibrary; | ||
|
||
$library = new YourLibrary(); | ||
echo $library->greet("John"); | ||
|
||
echo YourLibrary::helloWorld(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
sidebar_position: 3 | ||
title: Libraries | ||
--- | ||
|
||
Libraries | ||
========= | ||
Libraries are sets of functions that can be accessed by other extensions. They serve to offer a collection of features that other extensions can utilize. | ||
|
Oops, something went wrong.