PHPackages                             ashish336b/carpo-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ashish336b/carpo-php

ActiveLibrary

ashish336b/carpo-php
====================

This is small php framework for api based development

v1.1.8(5y ago)5333[2 issues](https://github.com/ashish336b/PhpClosureBasedFramework/issues)1PHP

Since Nov 1Pushed 5y ago1 watchersCompare

[ Source](https://github.com/ashish336b/PhpClosureBasedFramework)[ Packagist](https://packagist.org/packages/ashish336b/carpo-php)[ RSS](/packages/ashish336b-carpo-php/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (15)Used By (1)

PhpClosureBasedFramework
========================

[](#phpclosurebasedframework)

This is simple php closure based framework for api development

- [Installation](#Installation)

- [Routes](#Routes)
- [Controller](#Controller)
- [Model](#Model)
- [Database](#Database)
- [Migration](#Migration)
- [Middleware](#Middleware)
- [Request](#Request)
- [Response](#Response)
- [views](#views)

Installation
============

[](#installation)

```
composer create-project ashish336b/carpo-php-framework blog

```

#### With this Installation is completed. Run the command to open web server in port 1212.

[](#with-this-installation-is-completed-run-the-command-to-open-web-server-in-port-1212)

```
php -S localhost:1212 -t public/
```

#### Project Folder Structure.

[](#project-folder-structure)

├───app
│ ├───controller
│ ├───middleware
│ ├───model
│ └───views
├───public
└───vendor

- app/controller contains all your controller class for project
- app/middleware contains all your middleware class fro project.
- app/views contains all views files for project.

#### with this Full installation is completed.

[](#with-this-full-installation-is-completed)

Routes
======

[](#routes)

You can create routes by calling static class `Application`

```
use ashish336b\PhpCBF\Application as App;
App::get("/", function (Request $request, Response $response) {
   echo "My First Route.";
});
```

Methods

```
App::get("/urlPattern" , function(){});
App::post("/urlPattern" , function(){});
App::put("/urlPattern" , function(){});
App::delete("/urlPattern",function(){});
```

#### There is also another method you can access from `\ashish\PhpCBF\Application` class

[](#there-is-also-another-method-you-can-access-from-ashishphpcbfapplication-class)

```
App::on("EVENT_TYPE" , function(){});
```

#### EVENTTYPE can be either *BEFORE* or *AFTER*

[](#eventtype-can-be-either-before-or-after)

- BEFORE : This event run before all application middleware and routes. Best usecase to set CORS header.
- AFTER : If you need to run a piece of code after running your middleware and routes function use this. For example global response header is set here

### URL Pattern

[](#url-pattern)

- Url pattern is used to define routes. Both static pattern and dynamic pattern can be defined mostly same as laravel.
- #### static Routes: `App::get("/users", Closure);`

    [](#static-routes-appgetusers-closure)

    `baseurl/users` url is dispatched with this pattern.

- #### Variable Routes: `APP::get("/home/{id}", closure) `baseurl/home/1`or`baseurl/home/2` ... is dispatch.

    [](#variable-routes-appgethomeid-closure-baseurlhome1orbaseurlhome2--is-dispatch)
- You cannot register two same pattern.

#### Optional Pattern.

[](#optional-pattern)

- `App::get("/user/{id?}" , closure);`
    both `baseurl/user/1` and `baseurl/user` are dispatched here id params is optional.

- You should not define any required placeholder/params after optional placeholder/params.
    `/user/{id?}/{userId}` This is completely wrong.
    `/user/{userId}/{id?}` This is correct way.
- It is good practice to have only one optional params in pattern and it must be last placeholder.
- However two or more optional params are supported if only one required params appears before any optional params. Using more than one optional params may lead to confusion and hard to debug so is not recommended
- You cannot register static Routes after any variable routes that matches static route pattern. Example:
    suppose this variable pattern is defined at first`user/{id}` and you define another pattern `user/home` which matches``user/{id}` It gives you error.
- However you can define `user/home` at first and then `user/{id}`. This is completely fine.

#### Routes Group

[](#routes-group)

- Your can define routes group just like in other famous framework like laravel, slim. etc.

- First Params is array which can have two keys params and middleware.
- Params : For defining common url pattern that appears in every routes group.
- Middleware : name of middleware class that is inside `/App/middleware/` namespace.

```
App::group(["prefix"=>"/admin"],function(){
   // every GET POST PUT DELETE methods routes can be define.
   App::get("/login",Closure);
   //dispatch /admin/login
   App::get("/login/{id}", Closure);
   //dispatch /admin/login/1 , /admin/login/anything etc.
});
```

- `get`, `post` , `put` and `delete` method have two parameters. url pattern and closure respectively.
- closure can have two paramas $request and $response of type `Request` and `Response` respectively

```
App::get("/user/{id}/{anotherParams}", function (Request $request, Response $response) {
   // Access params value with $request->params.
   $request->params->id;
   $request->params->anotherParams;
});
```

Controller
==========

[](#controller)

You can create controller class inside app/controller with namespace `namespace App\controller;`

```

MVC Framework

```

- Example : include header.php inside views/partials/header.php.
- Extend method is used for extending layouts. This replace {{any\_string}} with content that comes after calling extend() method and before end() method in views file Example. layout.php

```
