PHP code example of sigawa / mvc-core

1. Go to this page and download the library: Download sigawa/mvc-core 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/ */

    

sigawa / mvc-core example snippets


$app->router->get('/', [HomeController::class,'index']);
$router->post('/submit', [HomeController::class,'functioname']);
//$router->get('/get/${id}/',[HomeController::class,'functioname']);

namespace App\Controllers;

use sigawa\mvccore\Request;
use sigawa\mvccore\Response;
use sigawa\mvccore\Controller;

class ControllernameController extends Controller
{
    public function index(Request $request, Response $response)
    {
        $this->setLayout('layoutname');
        // return $this->render($view, $params = [] optional, $layoutDirectory = '' optional);
        // by default, your layouts will be in the App/views/layout
        return $this->render('home');
    }
}

namespace App\Models;

use sigawa\mvccore\db\DbModel;

class permission extends DbModel
{
    public string $PermissionName ='';
    public string $Description  ='';

    public static function tableName(): string
    {
        return 'permission';
    }
    public function attributes(): array
    {
        return ['PermissionName','Description'];
    }

    public function rules()
    {
        return [
            'attributename'=>[self::RULE_REQUIRED],
            // other attributes and rules, explore the multiple rules in the Base method
           
        ];
    }
    public function save()
    {
        return parent::save();
    }
}

namespace namespace\Controllers;

use Sigawa\Hcp\models\permission;
use sigawa\mvccore\Application;
use sigawa\mvccore\db\CRUD;
use sigawa\mvccore\Request;
use sigawa\mvccore\Controller;

class PermissionsController extends Controller
{
    private $crud;
    private $permision;
    public function __construct(){
        $this->crud =new CRUD(Application::$app->db);
        $this->permision =new permission();
    }
    public function index()
    {
        // Logic for your index method goes here
        $this->setLayout('authenticated');
        return $this->render('permissions');
    }
    public function loadpermission(Request $request)
    {
        if($request->getMethod()==='get'){
            $data =$this->crud->getAll('permission','*',[]);
            echo json_encode($data);
        }
    }
    public function update(Request $request)
    {
        if($request->getMethod()==='post')
        {
            $input = json_decode(file_get_contents('php://input'), true);
            $description = $input['Description'];
            $id =$input['id'];
            $data =['Description' =>$description];
            $condition= ['id'=>$id];
            $updateResult =$this->crud->update('permission',$data,$condition);
            if ($updateResult['success']) {
                if ($updateResult['changesMade']) {
                    return true;
                } else {
                    echo json_encode("You did not make any changes");
                }
            } else
                {
                echo json_encode('Update Failed. Kindly make sure you typed the right data');
            }
        }
    }
}
bash
php mcconsole make:model User
bash
php mcconsole make:model User
bash
php mcconsole serve
bash
php mcconsole make:controller Controllername
bash
php mcconsole make:model Modelname