phpgram/mvc

A php mvc framework build on top of phpgram

1.5.3 2020-08-07 10:19 UTC

This package is auto-updated.

Last update: 2022-01-07 17:25:33 UTC


README

phpgram mvc project

Packagist Version PHP from Packagist Packagist

A fast and lightweight Mvc framework

Build on top of phpgram micro framework

Features

  • Simple and fast Http Routing

  • Psr 7 Request and Response with Nyholm

  • Psr 15 Middleware support

  • Psr 17 Factory with Nyholm

  • Dependency Injection with Psr 11 Container. Container from pimple

  • Php Template System

  • Basic Login, Authentication and Usermanagement via Php Session (or Psr 16 Cache)

  • Basic Psr 7 Cookies

  • Async Requests ready

Installation

via composer

composer create-project phpgram/mvc

Initialize / Set up

Local

  1. copy file: env.local.php.dist and rename it to: env.local.php

  2. complete the Database and Path information

    • for ROOT_URL_PATH = the relative Url Path (e.g.: hello.de/my_folder/ -> /my_folder is the path)
  3. done :)

Deploy

Change the env.php to your deploy server

Documentation

Define Routes

<?php
//file /routes/web.routes.php

use Gram\Project\App\AppFactory as Route;

use App\Http\Controller\DummyController;

//With function as handler
// for Url: / its return: This page is the Start
Route::get("/",function (){
	return "This page is the Start";
});

//with Controller (Class) as handler
//at Url: /controller: instantiate class DummyController (incl Dependency Injection) and call the method dummyFunction()
Route::get("/controller",DummyController::class."@dummyFunction");

//call this method with the value of id
Route::get("/user/{id}",DummyController::class."@dummyFunctionWithArgs");

Define Controller

<?php
//file: app/Http/Controller/DummyController.php

namespace App\Http\Controller;

use Gram\Mvc\Lib\Controller\BaseController;

class DummyController extends BaseController
{
	private $tpl = 'index.temp';	//template without the .php!

	public function dummyFunction()
	{
		return $this->view($this->tpl,[
			'msg'=>"Test Controller"
		]);
	}
	
	public function dummyFunctionWithArgs($id)
	{
		return $this->view($this->tpl,[
			'userId'=>$id
		]);
	}
}

Define Templates

<?php
	//file index.temp.php
$this->extend('defaultview'); //Values will being save for defaultview

$this->assign('h1',"Headline");	//assign variable h1 with the value Headline

//assign multiple variables at once
$this->assignArray([
	'title'=>"Test page",
	'my_awesome_button'=>"<button>Button</button>"
]);
?>

<?php 
//Starts a section. The content will be buffered
$this->start();?>
	<script type="text/javascript">
		alert("Hello! I am an alert box!!");
	</script>

	<style></style>
<?php 
//the content of this section will be available as variable head
$this->end('head');?>


<?php $this->start();?>
	<h2>Test Template</h2>
	
	<p>Welcome to phpgram mvc framework!</p>
<?php $this->end('content');?>

<?php
	//file defaultview.php
?>

<html>

<head>
	<?= $title?>
	<?= $head?>
</head>

<body>
	<h1><?= $h1?></h1>
	
	<div class="content">
		
		<p> <?= $content?> </p>
    	
    	<?= $my_awesome_button?>
    	
	</div>
</body>

</html>

License

phpgram is open source and under MIT License