Download the PHP package ashish336b/carpo-php without Composer
On this page you can find all versions of the php package ashish336b/carpo-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ashish336b/carpo-php
More information about ashish336b/carpo-php
Files in ashish336b/carpo-php
Package carpo-php
Short Description This is small php framework for api based development
License
Informations about the package carpo-php
PhpClosureBasedFramework
This is simple php closure based framework for api development
-
Installation
-
Routes
- Controller
- Model
- Database
- Migration
- Middleware
- Request
- Response
- views
Installation
With this Installation is completed. Run the command to open web server in port 1212.
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.
Routes
You can create routes by calling static class Application
Methods
There is also another method you can access from \ashish\PhpCBF\Application
class
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 is used to define routes. Both static pattern and dynamic pattern can be defined mostly same as laravel.
-
static Routes:
App::get("/users", Closure);
baseurl/users
url is dispatched with this pattern. -
Variable Routes:
APP::get("/home/{id}", closure)
baseurl/home/1or
baseurl/home/2` ... is dispatch. - You cannot register two same pattern.
Optional Pattern.
-
App::get("/user/{id?}" , closure);
bothbaseurl/user/1
andbaseurl/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 patternuser/home
which matches`user/{id}
It gives you error. - However you can define
user/home
at first and thenuser/{id}
. This is completely fine.
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. get
,post
,put
anddelete
method have two parameters. url pattern and closure respectively.- closure can have two paramas $request and $response of type
Request
andResponse
respectively
Controller
You can create controller class inside app/controller with namespace namespace App\controller;
you can define class and method to execute in route with classname@methodname.
-
In routes "AdminController@user" means AdminController is directly inside controller folder which have user method that is to be executed for response.
- If your controller is inside /app/controller/user/UserController.php and method is index that is to be executed then
Model
This contains data related logic such as retriving data from database and passing to controller.
you have to create model class inside model folder. This folder can contain sub folder to group models.
To access model function from controller.
Here Auth
is the name of file that is inside model folder and fetchUser()
is method inside Auth file/class.
In above case Auth class is inside Admin folder which is inside model folder.
Database
This framework mainly support mysql database. However you can integrate any database with the help other open source project.
-
To fetch all data from table(eg. user)
-
To run any query
- count result
Migration
you can use migration to run create table, alert etc. every migration should be inside app/migration directory.
every migration should be created in this manner. Till now sub directory is not allowed to store migration. Every migration should be in app/migrations directory.
Middleware
Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For Example if you want to check if user is authenticated or not you can check it in middleware. If user is not authenticated you can throw response of 403 which does not allow routes closure to execute.
Your can define Middleware in routes groups as well as individual routes.
Auth class should be inside app/middleware/Auth.php with namespace namespace app\\middleware\\
;
-
Middleware should always return true if none of the condition meets.
- You can define middleware in individual middleware in get,post,put and delete method.
Request
Request and Response can be accessed in both controller method and closure from parameter.
-return url without get params. eg. /admin/index
-
Return all body from post request as object.
-
Access Body with name. If BODY_NAME is in object of
$request->body
then it return value else returnnull
. -
Get all get params from url as object.
-
Return get query params value. If
QUERY_NAME
is not set it returnnull
-
return all request headers as object.
- Return header value. If not set return false
Response
Response class have two method as of now.
-
Return object to json.
-
render method return view containing html files. Accept two params first is path of folder from view folder and another is associative array.
/admin
is file from/app/view/admin.php
/admin/login
is file from/app/view/admin/login.php
views
-
views have three method render() , include() and extend().
- render() is already explainded in Response documentation.
-
include() method is called inside view file to include files. Usually done for including header and footer.
views/admin.php
- 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
-
Extending above layout.php view file.
- replace
{{any_string}}
with<h1>Body </h1>
License & copyright
Copyright (c) Ashish Bhandari
Licensed under the MID License.