, (*1)
Framework PHP
- This is documentation for Framework. MyFramework is a small Framework PHP experimental. He use a Dependency Injection Container:
$container = new Services\Container;
$container['engine'] = function($c){
return new Engine;
};
You can also make a static instance of your dependency:, (*2)
$container['model'] = $container->asShared(function ($c) {
return new Model($c['connection']);
});
- This Container is injected into the Services\Controller
Installation
Before using MyFramework in your project, add it to your composer.json
file:, (*3)
composer require myframework/myframework
Alternatively, MyFramework is also available as git clone, (*4)
$ git clone https://github.com/Antoine07/myFramework
Initialize
This section motivates and explains MyFramework's use of DI., (*5)
You have only define configuration in app.php. And you use simple Container to configure your application., (*6)
Routing configuration
You will define most of the routes for your app in the config/routes.yaml
file, which is loaded by the Container:, (*7)
$container['routes'] = Yaml::parse(ROUTES_PATH . '/routes.yaml');
$container['router'] = function ($c) {
$router = new Router;
foreach ($c['routes'] as $route) {
$router->addRoute(new Route($route));
}
return $router;
};
Basic routing
BlogController_index:
pattern: \/
connect: Controllers\BlogController:index
Route parameters
Sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a post's ID from the URL.
You may do so by defining pattern and params, (*8)
BlogController_show:
pattern: \/post\/(?P<id>[1-9][0-9]*)
connect: Controllers\BlogController:show
params: id
You may define as many route parameters as required by your route:, (*9)
CategoryController_show:
pattern: \/cat\/[a-zA-Z0-9\-_]+\/(?P<cat_id>[1-9][0-9]*)\/(?P<user_id>[1-9][0-9]*)
connect: Controllers\CategoryController:show
params: cat_id, user_id
Alternatively, you may define route with array PHP into app.php, (*10)
$routes = [
'BlogController_index' =>
[
'pattern' => '\/post\/(?P[1-9][0-9]*)'
'connect' => 'Controllers\BlogController:show'
'params' => 'id'
]
];
$container['routes'] = $routes;
RESTful Resource
This code, in the file routes.yaml, declaration creates multiple routes to handle a variety of RESTful actions on the student resource:, (*11)
StudentController:
resource: \/student
connect: Controllers\StudentController
action: '*'
Actions handled by resources Controller, (*12)
Verb |
Path |
Action |
GET |
/student |
index |
GET |
/student/create |
create |
POST |
/student |
store |
GET |
/student/{id} |
show |
PUT/PATCH |
/student/{id}/edit |
update |
DELETE |
/student/{id} |
destroy |
In your application PHP, you can use verb POST for PUT/PATCH and DELETE with hidden post value:, (*13)
// PUT/PATCH
$_POST['_method']='PUT'; // $_POST['_method'] = 'PATH';
// DELETE
$_POST['_method'] = 'DELETE';
Model
Active record pattern, (*14)
$Post = $this->pdo->setObject('Models\\Post');
$Post->id = 1;
$Post->title = 'hello world';
$Post->save();
$posts = $Post->all();
Template
all template views are compiled into plain PHP code. The template view file use the .php extension, and are typically stored in the resources/views directory, (*15)
Defining a layout
Layout can be defined in the application controller, is stored in the resources/views/layouts, (*16)
protected $layout = 'layouts.master';
In an application controller you can define a partial view, (*17)
$this->view->setRender('layouts.partials.header', []);
{{$header}}
<body>
{{$content}}
</div>
{{$footer}}
</body>
</html>
define a child page
main contain
@if(isset($posts))
@foreach($posts as $post)
-
id: {{$post->id}}
@endforeach
@endif