dinhthibc/feazy

A simple PHP framework

1.0.3 2018-11-04 04:21 UTC

This package is auto-updated.

Last update: 2024-10-04 17:47:17 UTC


README

A simple & lightweight PHP framework to build Web application & RESTful API

Installation

$ composer require dinhthibc/feazy

Simple app

Add simple index.php file:

<?php
define('ROOT_PATH', __DIR__);
$loader = require ROOT_PATH . '/vendor/autoload.php';

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
$router->get('/', function() {
	echo 'Welcome to Feazy';
});
?>

Then run server

$ php -S localhost:8000

Open localhost:8000 in web browser

Simple app with controllers

Add folder

$ mkdir src/Application/Controller

Add UserController class to controller folder

<?php

namespace Application\Controller;

use Feazy\Controller\Controller;

class UserController extends Controller
{
	public function index() {
		echo 'This is users page';
	}

	public function get($id) {
		echo "Get user details with id = $id";
	}
}
?>

And index.php file

<?php
define('ROOT_PATH', __DIR__);

$loader = require ROOT_PATH . '/vendor/autoload.php';
$loader->addPsr4('', ROOT_PATH . '/src');

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
$router->get('/', function() {
	echo 'Welcome to Feazy';
});

$router->group('/users', function(\Feazy\Common\Router $router) {
	$router->get('/', 'UserController@index');
	$router->get('/(\d+)', 'UserController@get');
});

?>

Then run server

$ php -S localhost:8000

Open localhost:8000/users or localhost:8000/users/1 in web browser

Router

This section will help you to under stand how to configure your router

Install new router

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);

You can use basic route like this:

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
$router->basicRoute();

Or define your own routes like this

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
$router->group('/users', function(\Feazy\Common\Router $router) {
	$router->get('/', 'UserController@index');
	$router->get('/(\d+)', 'UserController@get'); //using regex
});

$router->group('/api', function(\Feazy\Common\Router $router) {
	$router->setPrefix('\\Application\\API\\'); //set namespace for controller
	$router->get('/users', 'UserController@index');
});

View

Render layout

This section will help you to structure your view files with 1 layout file Let's have files structure like this

.
├── index.php
├── src
│   └── Application
│      └── Controller
│         └── IndexController.php
├── template
│   └── layout
|      └── layout.phtml
│   └── index
|      └── index.phtml
└── vendor

Put content for each file with following code: index.php

<?php
define('ROOT_PATH', __DIR__);

$loader = require ROOT_PATH . '/vendor/autoload.php';
$loader->addPsr4('', ROOT_PATH . '/src');

$router = new \Feazy\Common\Router($_SERVER['REQUEST_URI']);
\Feazy\Common\DIManager::add('view', new \Feazy\Common\View('template'));

$router->get('/', 'IndexController@index');
?>

IndexController.php

<?php
namespace Application\Controller;
use Feazy\Controller\Controller;
class IndexController extends Controller
{
	public function index() {
		$this->view->setTitle('Homepage');
		$this->view->render('index/index', [
			'myVariable' => 'This is a variable value'
		]);
	}
}

layout.phtml

<html>
<head>
	<!--- css/meta -->
	<title><?php echo $this->title; ?></title>
</head>
<h3>Layout title</h3>
<div class="content">
	<?php require_once ($this->content); ?>
</div>
</html>

index.phtml

myVariable => <?php echo $myVariable; ?>

When you run website on browser, you will get this result

Layout title
myVariable => This is a variable value

Render view section

Manage your common section in multiple child pages Now we have some extra files in directories

.
├── index.php
├── src
├── template
│   └── layout
│   └── index
|      └── index.phtml
│   └──  sections
│      └── component1.phtml
│      └── component2.phtml
└── vendor

Update index.phtml code

<div>
	<?php $this->addSection('sections/component1'); ?>
</div>
<div>
	<?php $this->addSection('sections/component2', ['myVariable']); ?>
</div>

component1.phtml

This is component 1 content

component2.phtml

myVariable => <?php echo $myVariable; ?>

View variables

Help us to expose a variable from Controller to View

class IndexController extends Controller
{
	public function index() {
		$this->view->setTitle('Homepage');
		$this->view->render('index/index', [
			'myVariable' => 'This is a variable value'
		]);
	}
}

In view child page just call block below to get $myVariable value

<?php echo $myVariable; ?>

.... to be continued