PHP code example of ashish336b / carpo-php

1. Go to this page and download the library: Download ashish336b/carpo-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ashish336b / carpo-php example snippets


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

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

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

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.
});

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



namespace App\controller;

use ashish336b\PhpCBF\Request;
use ashish336b\PhpCBF\Response;

class AdminController
{
  public function index(Request $request, Response $response)
  {
     $request->url = $request->getUrl();
     return $response->toJSON($request);
  }
  public function user(Request $request, Response $response)
  {
     echo $response->render("/admin", ['fullUrl' => $request->fullURL]);
  }
}

App::get("/admin/user", "AdminController@user");

App::get("/user", "user\UserController@index")



namespace App\model;

use ashish336b\PhpCBF\DB;
use ashish336b\PhpCBF\Model;

class Auth extends Model
{
   protected $table = "user";
   public function fetchUser()
   {
      //directly run query from model
      return $this->query("select * from user")->results();
      // fetch all row of table specified in $table
      return $this->fetch();
      // get columns of table
      return $this->getColumns();
   }
}

$user  = $this->model("Auth")->fetchUser();

$this->model("Admin\Auth")->fetchUser();

DB::table("user")->get();

$result = DB::raw()->query("select * from user where id =  ?",[1])->result();

$noOfRow = DB::raw()->query("select * from user")->count();



namespace App\migrations;

use ashish336b\PhpCBF\Application;

class create_user_table
{
   public function up()
   {

      $SQL = "CREATE TABLE users (
               id INT AUTO_INCREMENT PRIMARY KEY,
               firstname VARCHAR(255) NOT NULL,
               lastname VARCHAR(255) NOT NULL,
               email VARCHAR(255) NOT NULL,
               created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
               updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )";
   Application::$pdo->exec($SQL);
   }

   public function down()
   {
      $SQL = "DROP TABLE users;";
      Application::$pdo->exec($SQL);
   }
}

App::group(["prefix"=>"/admin","middleware"=>["Auth"]],function(){
   App::get("/",function(){
      echo "/admin";
   })
});



namespace App\middleware;

use ashish336b\PhpCBF\Request;
use ashish336b\PhpCBF\Response;

class Auth
{
   public function run(Request $request, Response $response)
   {
      $auth = false;
      if(!$auth){
         return $response->toJSON(["message" => "not authenticated"]);
      }
      return true;
   }
}

App::get("/user","UserController@index",["Guest"]);

$request->getUrl();

$request->body

$request->body("BODY_NAME");

$request->query

$request->query("QUERY_NAME");

$request->allHeaders();


$request->header("Header_NAME");

$response->toJSON($obj);

$response->render("/admin",array);

<body>
 $this->

 $this->

 $this->extend("any_string", "partials/layout"); 
composer
composer create-project ashish336b/carpo-php-framework blog
 echo $ok