Introducing Monsoon PHP Framework (monsoonphp.com). You can maintain your API and CLI code in the same codebase of your Application using the MVC pattern. The framework assists developers in creating a simple, fast and secure application very quickly. See this slideshow for an introduction. Proudly made in Bhārat (India).
3. Introduction to Monsoon Framework
• HMVC Pattern in PHP
• App, API and CLI scripts in one codebase
• Composer compatible (bring your own library)
• Docker ready
• Ready with essential tools
• PHP Unit, PHP Code Sniffer, PHP Mess Detector
• Open source and extensible
4. Who can use?
• Senior and Junior PHP developers
• Architects who want a fast, secure and performing code structure
• Students who want to understand MVC implementation in PHP
• Seasoned application developers
• Any PHP Programmer
5. Why Monsoon?
Monsoon is the framework for you, if
• you love core PHP and simple MVC, not a creative vocabulary
• you love HTML in view files, not any template engines
• you love plain SQL queries in the models, not objects
• you love automatic routing in your application, not a rule to define each route
• you want to take control of the entire script execution cycle, not some black box
• you need a light-weight and secure structure, not an extra-terrestrial folder structure
6. Kick Start
Just 2 steps to kick start
• Step – 1 : Get the code through Composer
• composer create-project monsoon/framework .
• Step – 2 : Start the webserver
• php –S localhost:8080 -t public/
7. Folder Structure
Top level
• src
• App
• Api
• Cli
• Config
• Framework
• bin
• data
• public
• resources
App/
• Classes
• Controllers
• Entities
• Factories
• Helpers
• Interfaces
• Layouts
• Models
• Modules/
• Tests
• Traits
• views
Modules/
• Controllers
• Models
• views
Note
• Api, App, Cli, Config, Framework folder names have the first
letter in uppercase, as they contain classes and need to be
PSR-4 compliant
• src, views, bin, data, resources, public folders are in lower
case
Api/
• Services
• Tests
Config/
• Config.php
• .env.php
• .routes.php
9. Execution Flow
• Starts from public/index.php
• Calls src/App/Classes/Initialize.php
• Loads configuration from
• Config/Config.php
• Config/.env.php
• Config/.routes.php
• Automatically invokes your Controller based on the url or route defined
• Your Controller invokes your Models/Entities
• Renders HTML from your view file
• Done !!
14. The “Box”
• A class just to hold information
• Contains
• $data
• $identity
• $config
• $container
• .. and variables assigned by you
• Accessible across all classes in a static way
• Box::$data[‘usersList’]
• “Just put it in the box”
18. Layouts
• Layout must have header.phtml and footer.phtml under
src/App/Layouts/<layout_name> folder
• ”default” layout is used by default
• Can be invoked from Controller
• View::renderDefault(‘users/index’);
• View::render(‘users/index’, ‘custom-layout’);
• Box’ed data can be used in the Layout
19. Modules
• Modules within your application for HMVC pattern
• Sets of Controllers, Models, views
• You can also add Interfaces, Helpers, Classes, etc.
• PSR-4 compliant
• Separate namespace
• e.g. namespace ModulesSettingsControllers;
20. Entities
src/App/Entities/User.php
<?php
namespace Entities;
class User extends FrameworkEntity
{
public function __construct()
{
parent::__construct();
$this->setTableName(‘users’);
$this->setIdField(‘user_id’);
}
}
Usage
$user = new EntitiesUser();
$user->first_name = ‘Krishna’;
$user->last_name = ‘Manda’;
$user->email_id = ‘krishna@example.com’;
$user->save();
// …
echo ‘User Id : ‘.$user->user_id;
23. APIs
• File name suffixed with ”Service.php”
• Similar to App/Controllers have “Controller.php” suffix
• Methods in Service are suffixed with “Action” word
• App/Controllers also use “Action”
• Methods in Service are prefixed with HTTP method
• public function getUserOperation($id)
• public function postUserOperation($userData)
• public function patchUserOperation($changedUserData)
24. API Example
src/Api/Services/UsersService.php
<?php
namespace ApiServices;
use ModelsUsersModel;
class UsersService extends FrameworkService
{
public function getUserOperation($userId)
{
$userData = (new UsersModel)->getUserDetails($userId);
$response = new FrameworkResponse();
$response->setHttpStatusCode('200', 'OK’);
$response->setData($userData);
$response->dispatchJson();
}
}
25. CLI Programming
• Use the popular Symfony’s Console component
(https://symfony.com/doc/current/components/console.html)
• Triggering scripts from bin/
• Classes in Cli/
• Flow
• Create class in Cli/ and include them in bin/console
• Running from terminal, an example
• $ php bin/console greet Krishna
26. Dependency Injection (Factory Pattern)
src/App/Factories/IndexControllerFactory.php
<?php
namespace Factories;
use ConfigConfig,
use ControllersIndexController;
use FrameworkContainer;
use FrameworkInterfacesFactoryInterface;
class IndexControllerFactory implements FactoryInterface
{
public static function create(Container $container)
{
return new IndexController($container->get(Config::class));
}
}
src/App/Controllers/IndexController.php
<?php
namespace Controllers;
class IndexController extends FrameworkController
{
public function __construct(Config $config)
{
$this->config = $config;
}
public function indexAction()
{
// ...
}
}
27. Database Migrations
• Phinx based migrations (http://docs.phinx.org/en/latest/)
• phinx.php – Configuration file
• Migration commands
• vendor/bin/phinx create MyNewMigration
• vendor/bin/phinx migrate
• Migrations are created under data/migrations
28. WritingTest Cases
• Support for PHPUnit (https://phpunit.readthedocs.io/en/8.2/)
• Test cases maintained under
• src/App/Tests
• src/App/Modules/<module_name>/Tests
• src/Api/Tests
• Commands
• vendor/bin/phpunit
29. Thank you!
• Try MonsoonPHP with a small POC
• Visit Monsoon PHP website for more video tutorials
• https://monsoonphp.com