https://github.com/alexdodonov/mezon-common-application
https://github.com/alexdodonov/mezon-common-application
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/alexdodonov/mezon-common-application
- Owner: alexdodonov
- Created: 2021-02-10T09:56:41.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-27T15:13:34.000Z (over 3 years ago)
- Last Synced: 2025-06-01T00:41:09.824Z (5 months ago)
- Language: PHP
- Size: 40 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Common application class
### Intro
This class provides simple application routine with more complex rendering and error handling.
### Extended routes processing
In [Application](https://github.com/alexdodonov/mezon-application) class routes may return only strings. But CommonApplication class allows you to return arrays of string which will be placed in the template placeholders.
Simple example:
```PHP
class ExampleApplication extends CommonApplication
{
/**
* Constructor.
*/
function __construct( $template )
{
parent::__construct( $template );
}function actionSimplePage()
{
return [
'title' => 'Route title' ,
'main' => 'Route main'
];
}
}
```Here route's handler generates two parts of the page /simple-page/ - 'title' and 'main'. These two part will be inserted into {title} and {main} placeholders respectively.
More complex example:
```PHP
class ExampleApplication extends CommonApplication
{
/**
* Constructor.
*/
function __construct($template)
{
parent::__construct($template);
}function actionSimplePage()
{
return [
'title' => 'Route title' ,
'main' => new View('Generated main content')
];
}
}
```Here we pass instance of the class View (or any class derived from View) to the application page compilator. It will call View::render method which must return compiled html content.
### Routes config
You can also keep al routes in configs. You can use json configs:
```JS
[
{
"route": "/route1/",
"callback": "route1",
"method": "GET"
},
{
"route": "/route2/",
"callback": "route2",
"method": ["GET","POST"]
}
]
```This data must be stored in the './conf/' dir of your project. Or load configs explicitly as shown below (using method loadRoutesFromConfig).
And we also need these methods in the application class.
```PHP
class ExampleApplication extends CommonApplication
{
/**
* Constructor.
*/
function __construct($template)
{
parent::__construct($template);// loading config on custom path
$this->loadRoutesFromConfig('./my-routes.json');
}function route1()
{
return [
// here result
];
}function route2()
{
return [
// here result
];
}
}
```Note that you can load multiple configs with one call of the method loadRoutesFromConfigs
```PHP
function __construct($template)
{
parent::__construct($template);$this->loadRoutesFromConfigs(['./conf/my/routes.json', './conf/my-routes.php']);
}
```Or the same:
```PHP
function __construct($template)
{
parent::__construct($template);$this->loadRoutesFromDirectory('./conf');
}
```