PHP Classes

Define MVC PHP 8: Framework to route requests via a front controller

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 165 All time: 8,914 This week: 51Up
Version License PHP version Categories
define-mvc-php-8 1GNU General Publi...8HTTP, Libraries, Design Patterns, PHP 8
Description 

Author

This package provides a framework to route requests via a front controller.

It implements MVC based framework with a front controller class that parses the URLs of all requests and routes them to specific controller classes.

The framework can load an application configuration script to determine the path of files that define the application controllers, views, and other application details.

Picture of Nitesh Apte
  Performance   Level  
Name: Nitesh Apte <contact>
Classes: 18 packages by
Country: India India
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Example

<?php
declare(strict_types = 1);

use
Define\Core\Framework;
use
Define\Core\RegisterObject;
use
Define\Core\Router;
use
Define\Core\Session;
use
Define\DBDrivers\DatabaseBean;
use
Define\Exceptions\FrameworkException;
use
Define\Utilities\Logger;

/**
 * BOOTSTRAP
 *
 * Initialization script. Front Controller
 *
 * @category Define
 * @package /
 * @author Nitesh Apte <me@niteshapte.com>
 * @copyright 2015-2021 Nitesh Apte
 * @version 2.0.0
 * @since 1.0.0
 * @license https://www.gnu.org/licenses/gpl.txt GNU General Public License v3
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

ini_set('display_errors', "1");

ob_start();
session_start();
session_regenerate_id();


# Access to all the pages will be possible only through bootstrap. After all this is a front controller based MVC Framework.
const DIRECT_ACCESS = true;

# Settings for framework - Start #
# Set the input paths.
# Framework path
set_include_path(get_include_path().PATH_SEPARATOR."lib/define/core");
set_include_path(get_include_path().PATH_SEPARATOR."lib/define/dbdrivers");
set_include_path(get_include_path().PATH_SEPARATOR."lib/define/exceptions");
set_include_path(get_include_path().PATH_SEPARATOR."lib/define/traits");
set_include_path(get_include_path().PATH_SEPARATOR."lib/define/utilities");

include_once
'configuration/define.php';

# Settings for framework - End #

# Settings for application - Start #
# Set the input paths.
# Application path
set_include_path(get_include_path().PATH_SEPARATOR."application/controller");
set_include_path(get_include_path().PATH_SEPARATOR."application/service/");
set_include_path(get_include_path().PATH_SEPARATOR."application/repository/");
set_include_path(get_include_path().PATH_SEPARATOR."application/dto");
set_include_path(get_include_path().PATH_SEPARATOR."application/exceptions");


/**
 * Autoload method for dynamically loading classes.
 *
 * @param string $object Name of Class
 * @return void
 */
function autoload(string $object) {
   
$split = explode("\\", $object);
   
$className = end($split);
    require_once(
"$className.php");
}

spl_autoload_register("autoload");

try {
   
$container = RegisterObject::getInstance();
   
$container->offsetSet('FRAMEWORK', Framework::getInstance());
   
$container->offsetSet('SESSION', Session::getInstance());
   
$container->offsetSet('LOGGER', Logger::getInstance());
   
$container->offsetSet('MARIADB', new DatabaseBean("MARIADB", "host", "username", "pass", "schema", "3306", "db"));
   
$session = $container->offsetGet('SESSION');
   
$default = array();
   
$container->offsetGet('FRAMEWORK')->init(Router::getInstance(), $default);
} catch (
FrameworkException $e) {
    echo
$e;
}


Details

Define MVC PHP 8

Define MVC is a *Front Controller* based light weight MVC framework for developing web based applications. It is an open source and will remain always.

Define MVC PHP 5.3 - 7.2 ###

First release of Define MVC supports PHP 5.3 to PHP 7.2. You can get it from <a href="https://github.com/niteshapte/define-mvc/">*HERE*</a>.

Contribute

Please feel free to contribute. But try to keep the implementation simple so that a developer working for first time shouldn't find it difficult to understand and use it.

Requirement

PHP 8.0+

Virtual Host

While developing I created a virtual host for this framework. Request you guys to do the same. Probably it will not work if you try to access it like _htt<area>p://localhost/define-mvc-php-8/_.

Creating a Virtual Host

Add below line in /etc/apache2/apache2.conf <br> Include vhost.conf

Create a file vhost.conf in /etc/apache2/ folder and add below lines - <pre> NameVirtualHost *:80

&lt;VirtualHost *:80&gt;

ServerAdmin admin@define.mvc
ServerName define.mvc
ServerAlias www.define.mvc
DocumentRoot /var/www/html/define-mvc-php-8/
&lt;Directory "/var/www/html/define-mvc-php-8"&gt;
    AllowOverride All
&lt;/Directory&gt;
DirectoryIndex bootstrap.php

&lt;/VirtualHost&gt; </pre>

Add below line in /etc/hosts file - <br> 127.0.0.1 www.define.mvc

Run below command on terminal to restart apache - <br> service apache2 restart

_Please note above configurations were done in my linux machine. You must do according to your setup._

How to use it

Define MVC is a front controller based MVC framework just like any other MVC framework. So, all the request are redirected to bootstrap.php page. Please have a look at .htaccess file.

Folder Structure

Please don't skip this section. Go through the below folder structure to understand how Define MVC and project / application classes and files will be organized.

define-mvc-php-8
|  - README.md
|  - bootstrap.php      // Front controller file. All request are redirected to this file. Redirect configuration is in .htaccess
|  - .htaccess   
???? application        // project / application files
|   ???? controller     // controller files
|   ???? dto            // DTOs related to project
|   ???? exceptions     // Exceptions related to projects. 
|   ???? i18n           // Internationalization / Localization
|   ???? repository     // DAO classes of project
|   ???? service        // Service classes of project
|   ???? view           // view / html / html + php files
???? configuration      
|   - application.php   // configuration for project
|   - define.php        // configuration for framework
???? docs               // contains configuration files for both application and framework 
???? lib                
|   ???? define             // DEFINE MVC files
|   |   ???? core           // Framework's core classes
|   |   ???? dbdrivers      // Database Driver classes
|   |   ???? exceptions     // Exceptions for framework 
|   |   ???? traits         // Traits for frameork
|   |   ???? utilities      // Utility classes for framework
|   ???? vendors            // vendor libraries
|       ???? phpmailer      
|       ???? json-object-mapper
|       ???? etc.
???? logs                   // logs generated by logger
???? scripts (optional)     // Scripts like DB scripts or shell scripts
    ???? db
    ???? shell

All the files related to your project will be inside 'application' folder. However, you can change the configurations defined in configuration/define.php.

Controller

By default, Define MVC follow below URL pattern:

_http<area>://www<area>.domain.com/controller/action/param1-param2/_

For example, if the URL is htt<area>p://www<area>.example.com/user/profile/33-90/, or you want to create this url, then craete UserController class in application/controller with below code.

<pre> class UserController extends ApplicationController {

public function profileAction(string $param1, string $param2) {
	// logic goes here
}

} </pre> Check IndexController inside application/controller to get more details.

View

All the view files will be inside application/view/ folder.

You can add display value in view by using View object. For example:

<pre> class UserController extends ApplicationController {

public function profileAction($param1, $param2) {
	$this->view->addObject("msg", "I am the value to be displayed.");
	$this->view->render('user');
}

} </pre>

In application/view/ folder, create a file named user.php, and add the following code:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8"/>
<title>Define MVC Index Controller with Index View</title>
<body>
<?php echo $msg ?? "No value set";?>
</body>
</html>

Repository

Refer to application/repository/IndexRepository class to get an idea how to query and fetch result from a database. Please note DB config should be set in bootstrap.php file.

As a good practice, call repository classes from service classes.

Service

Refer to application/service/IndexService class to get an idea how to call a repository class or do other things.

Test

After setting up define-mvc-php-8 in your local server, try accessing the following:

http://www.define.mvc

http://www.define.mvc/index/

http://www.define.mvc/index/default/me-you/

http://www.define.mvc/index/test-from-service/

Configuration

Define MVC is completely configurable.

For example, you want your UserController to be UserXYZ go to configuration/define.php and change CONTROLLER_SUFFIX to XYZ. Similarly, you can change other configuration properties.

TODO

Write unit tests


  Files folder image Files (61)  
File Role Description
Files folder imageapplication (6 directories)
Files folder imageconfiguration (2 files)
Files folder imagedocs (1 file)
Files folder imagelib (2 directories)
Files folder imagelogs (1 file)
Files folder imagescripts (2 directories)
Accessible without login Plain text file .htaccess Data Auxiliary data
Accessible without login Plain text file bootstrap.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (61)  /  application  
File Role Description
Files folder imagecontroller (3 files)
Files folder imageexceptions (1 file)
Files folder imagei18n (1 directory)
Files folder imagerepository (2 files)
Files folder imageservice (2 files)
Files folder imageview (2 files)

  Files folder image Files (61)  /  application  /  controller  
File Role Description
  Plain text file ApplicationController.php Class Class source
  Plain text file ErrorController.php Class Class source
  Plain text file IndexController.php Class Class source

  Files folder image Files (61)  /  application  /  exceptions  
File Role Description
  Plain text file ApplicationException.php Class Class source

  Files folder image Files (61)  /  application  /  i18n  
File Role Description
Files folder imageen (1 file)

  Files folder image Files (61)  /  application  /  i18n  /  en  
File Role Description
  Accessible without login Plain text file en.php Aux. Auxiliary script

  Files folder image Files (61)  /  application  /  repository  
File Role Description
  Plain text file ApplicationRepository.php Class Class source
  Plain text file IndexRepository.php Class Class source

  Files folder image Files (61)  /  application  /  service  
File Role Description
  Plain text file ApplicationService.php Class Class source
  Plain text file IndexService.php Class Class source

  Files folder image Files (61)  /  application  /  view  
File Role Description
  Accessible without login Plain text file error.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files (61)  /  configuration  
File Role Description
  Accessible without login Plain text file application.php Aux. Auxiliary script
  Accessible without login Plain text file define.php Aux. Auxiliary script

  Files folder image Files (61)  /  docs  
File Role Description
  Accessible without login Plain text file docs.txt Doc. Documentation

  Files folder image Files (61)  /  lib  
File Role Description
Files folder imagedefine (5 directories)
Files folder imagevendors (1 directory)

  Files folder image Files (61)  /  lib  /  define  
File Role Description
Files folder imagecore (15 files)
Files folder imagedbdrivers (9 files)
Files folder imageexceptions (7 files)
Files folder imagetraits (1 file)
Files folder imageutilities (7 files)

  Files folder image Files (61)  /  lib  /  define  /  core  
File Role Description
  Plain text file BaseController.php Class Class source
  Plain text file BaseDAO.php Class Class source
  Plain text file BaseModel.php Class Class source
  Plain text file BaseService.php Class Class source
  Plain text file Framework.php Class Class source
  Plain text file IBaseDAO.php Class Class source
  Plain text file IBaseModel.php Class Class source
  Plain text file IBaseService.php Class Class source
  Plain text file IDefine.php Class Class source
  Plain text file IRouter.php Class Class source
  Plain text file IView.php Class Class source
  Plain text file RegisterObject.php Class Class source
  Plain text file Router.php Class Class source
  Plain text file Session.php Class Class source
  Plain text file View.php Class Class source

  Files folder image Files (61)  /  lib  /  define  /  dbdrivers  
File Role Description
  Plain text file DatabaseBean.php Class Class source
  Plain text file DatabaseFactory.php Class Class source
  Plain text file IDatabase.php Class Class source
  Plain text file MariaDBDriver.php Class Class source
  Plain text file MySQLiDBDriver.php Class Class source
  Plain text file OracleDBDriver.php Class Class source
  Plain text file PDODBDriver.php Class Class source
  Plain text file PostgresDBDriver.php Class Class source
  Plain text file SQLiteDBDriver.php Class Class source

  Files folder image Files (61)  /  lib  /  define  /  exceptions  
File Role Description
  Plain text file CloneNotSupportedException.php Class Class source
  Plain text file FileNotFoundException.php Class Class source
  Plain text file FrameworkException.php Class Class source
  Plain text file IException.php Class Class source
  Plain text file NotSerializableException.php Class Class source
  Plain text file NullPointerException.php Class Class source
  Plain text file RuntimeException.php Class Class source

  Files folder image Files (61)  /  lib  /  define  /  traits  
File Role Description
  Plain text file SingletonTrait.php Class Class source

  Files folder image Files (61)  /  lib  /  define  /  utilities  
File Role Description
  Accessible without login Plain text file ErrorCodes.php Aux. Auxiliary script
  Plain text file ErrorExceptionHandler.php Class Class source
  Accessible without login Plain text file ExceptionCodes.php Aux. Auxiliary script
  Plain text file IErrorExceptionHandler.php Class Class source
  Plain text file IUtilities.php Class Class source
  Plain text file Localization.php Class Class source
  Plain text file Logger.php Class Class source

  Files folder image Files (61)  /  lib  /  vendors  
File Role Description
Files folder imagephpmailer (1 file)

  Files folder image Files (61)  /  lib  /  vendors  /  phpmailer  
File Role Description
  Accessible without login Plain text file congfig.php Aux. Auxiliary script

  Files folder image Files (61)  /  logs  
File Role Description
  Accessible without login Plain text file info.log Data Auxiliary data

  Files folder image Files (61)  /  scripts  
File Role Description
Files folder imagedb (1 file)
Files folder imageshell (1 file)

  Files folder image Files (61)  /  scripts  /  db  
File Role Description
  Accessible without login Plain text file mariadb.sql Data Auxiliary data

  Files folder image Files (61)  /  scripts  /  shell  
File Role Description
  Accessible without login Plain text file demo.sh Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:165
This week:0
All time:8,914
This week:51Up