A simple OOP template what i use for my different project
Download or clone the project in www folder of your local server
git clone https://github.com/Crash-Zeus/templatePoo.gitConfigure the config/config.php with your parameters if you want connect in your DB
return [
"db_user" => "your_DB_username",
"db_password" => "your_DB_password",
"db_host" => "your_DB_host",
"db_name" => "your_DBname"
];- Add your route in public/index.php :
// No params
$router->get('/routeName', 'controllerName.functionName');
// 1 param
$router->get('/routeName/:param', 'controllerName.functionName');
// 2 params
$router->get('/routeName/:param1/:param2', 'controllerName.functionName');
// etc...
// Or with parameter pattern (regEX)
$router->get('/routeName/:id', 'controllerName.functionName')->with('id', '([0-9]+)');
// Or same with controller in different folder (app/Controller/folderName/controllerName)
$router->get('/routeName','folderName.controllerName.functionName');- Add your controller in app/Controller :
<?php
namespace App\Controller;
// If your view is "test" the controller name is "TestController
class TestController extends AppController {
public function __construct() {
parent::__construct();
}
}- Add the corresponding view in app/Views :
<!-- The view name is "viewName".php in the router -->
<!-- She content the HTML of page you want to display like : -->
<h1>Hello world !</h1>- Create the function in your controller :
<?php
namespace App\Controller;
class TestController extends AppController {
public function __construct() {
parent::__construct();
}
// If your functionName is "testFunction" the function name is "testFunction()"
public function testFunction() {
// if you want transfer variable from here to view use compact()
// after you can use variable in view
$test = 1;
$somevariable = 'Yes';
$this->render('view', compact('test','somevariable'));
}
}- Go to route ! Enjoy !
- Repeat (just if you want separate view for the form) the steps 2 to 4
- Add the route on public/index.php :
$router->post('routeName', 'controllerName.functionName');Create the route you need and add an anonymous function with parameters and code you want to execute
$router->get('routeName/:param', function($param) {
echo "Execute anonymous function with param : {$param}";
});- You can create forms with the
BoostrapForm\BoostrapFormclass include in require - You can secure POST and GET vars with the
UserInput\BoostrapUserInputFormclass include in require (see UserInput.php for the how to use) - To start test with php test use
./vendor/bin/phpunitin racine folder (run into docker if your app is into container)
Pull requests are welcome ! You can fork the project to add your improvements.
It is an Work in progress project
This template is not perfect i know :)
But i want participate in git for give example where OOP php is use in an MVC arch
My code is not very very clean, but is not very dirt, i perfect my code in time ^^
And sorry if my English is not perfect, i work that ! i promise you !
Crash Zeus - 2019