Skip to content

EasyGo is a simple, lightweight PHP and JavaScript framework for building web applications following the MVC architecture.

Notifications You must be signed in to change notification settings

kavindadimuthu/PHP-JS-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EasyGo Framework

A simple, lightweight PHP and JavaScript framework for building web applications following the MVC architecture.

Overview

EasyGo is a minimalist PHP framework designed for simplicity and flexibility. It provides a clean structure for your web applications with essential features like routing, database abstraction, MVC architecture, template system, session management, and file handling.

Features

  • Routing System: Simple and flexible URL routing
  • MVC Architecture: Clean separation of concerns
  • Database Abstraction: Simple database operations with PDO
  • Template System: Clean view rendering with layouts
  • Session Management: Secure session handling
  • File Handling: Easy file upload and management
  • API Support: Built-in support for RESTful API development

Directory Structure

.
├── composer.json        # PHP dependencies
├── core/               # Framework core files
│   ├── Application.php  # Main application class
│   ├── BaseController.php # Base controller class
│   ├── Request.php     # HTTP request handling
│   ├── Response.php    # HTTP response handling
│   ├── Router.php      # URL routing
│   ├── Database/       # Database related classes
│   └── Utils/          # Utility classes
├── public/             # Publicly accessible files
│   ├── index.php       # Application entry point
│   └── assets/         # CSS, JS, images, etc.
└── src/                # Application source code
    ├── routes.php      # Route definitions
    ├── controllers/    # Application controllers
    ├── models/         # Application models
    └── views/          # View templates

Requirements

  • PHP 7.4 or higher
  • Composer
  • MySQL or compatible database

Installation

1. Create a new project

You can create a new EasyGo project in two ways:

Option 1: Clone the repository

git clone https://github.com/yourusername/easygo-framework.git my-project
cd my-project

Option 2: Download the ZIP file

Download the ZIP file from GitHub and extract it to your project directory.

2. Install dependencies

composer install

3. Configure environment

Copy the example environment file:

cp .env.example .env

Edit the .env file with your database and application settings:

# Database connection settings
DB_DRIVER = mysql
DB_HOST = localhost
DB_PORT = 3306
DB_NAME = your_database_name
DB_USER = your_database_user
DB_PASSWORD = your_database_password

# Application settings
APP_ENV = development
APP_DEBUG = true
APP_URL = http://localhost

4. Set up the database

Create a new database and import the schema if available.

5. Start the development server

composer start

This will start the PHP development server at http://localhost:5000.

Creating Your Application

1. Define Routes

Routes are defined in src/routes.php. A typical route looks like:

// Page routes (rendering views)
$app->router->get('/', 'SiteController@home');
$app->router->get('/users-page', 'SiteController@usersPage');

// API routes
$app->router->get('/api/users', 'UserController@getUsers');
$app->router->post('/api/users', 'UserController@createUser');

2. Create Controllers

Controllers are placed in src/controllers/. A basic controller looks like this:

<?php
// filepath: src/controllers/UserController.php
require_once __DIR__ . '/../../vendor/autoload.php';

use app\core\BaseController;
use app\core\Database\BaseModel;

class UserController extends BaseController
{
    private $userModel;

    public function __construct()
    {
        $this->userModel = new BaseModel('users');
    }

    // API endpoint to get users data
    public function getUsers($req, $res)
    {
        // Get users from database
        $users = $this->userModel->read();
        
        // Return JSON response
        $res->setHeader('Content-Type', 'application/json');
        echo json_encode([
            'status' => 'success',
            'data' => ['users' => $users]
        ]);
    }
}

3. Create Views

Views are placed in src/views/. You can use layouts for consistent page structure:

<?php
// In a controller method:
$this->renderView('pages/users/index', [
    'title' => 'Users List',
    'description' => 'View all users in the system'
]);

// Or with a layout:
$this->renderWithLayout('main', 'pages/users/index', [
    'title' => 'Users List',
    'description' => 'View all users in the system'
]);

4. Working with the Database

The framework provides a simple database abstraction layer:

// Create a model instance for a table
$userModel = new BaseModel('users');

// Create a new record
$userId = $userModel->create([
    'username' => 'john_doe',
    'email' => 'john@example.com'
]);

// Read records
$users = $userModel->read([
    'order' => 'username ASC',
    'limit' => 10
]);

// Update a record
$userModel->update(['status' => 'active'], [
    'filters' => ['id' => 5]
]);

// Delete a record
$userModel->delete([
    'filters' => ['id' => 5]
]);

Best Practices

  1. Use MVC Pattern: Keep controllers thin, business logic in models, and presentation in views.
  2. Validate Input: Always validate and sanitize user input.
  3. Use Prepared Statements: The database layer uses prepared statements to prevent SQL injection.
  4. Keep Routes Organized: Group related routes together.
  5. Use Layouts: Create reusable layouts for consistent page structure.

Architecture

Request Lifecycle

  1. User makes a request to public/index.php
  2. Application bootstraps and loads environment variables
  3. Router matches the URL to a route
  4. Controller action is executed
  5. Response is returned to the user

API Architecture

The framework supports a separation of rendering and data fetching:

  1. Pages are initially rendered without data
  2. JavaScript fetches data from API endpoints
  3. UI is updated with the fetched data
  4. This approach allows for better separation of concerns

Troubleshooting

Common Issues

  • White Screen of Death: Check your error logs and ensure APP_DEBUG is set to true during development.
  • Route Not Found: Ensure the route is properly defined in src/routes.php.
  • Database Connection Failed: Verify your database credentials in the .env file.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Credits

EasyGo Framework was created by Kavinda.

About

EasyGo is a simple, lightweight PHP and JavaScript framework for building web applications following the MVC architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published