dasunnethsara / zenithphp
ZenithPHP is a lightweight PHP framework for MVC architecture, streamlining modern PHP web development.
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Type:project
pkg:composer/dasunnethsara/zenithphp
Requires
- php: ^8.0
- dasunnethsara/zenithphp-core: ^1.0
- firebase/php-jwt: ^6.10
- vlucas/phpdotenv: ^5.6
README
ZenithPHP
Welcome to ZenithPHP, a simple and lightweight PHP framework designed to streamline web application development. ZenithPHP follows the MVC (Model-View-Controller) architecture and provides developers with a clean and flexible foundation to build modern PHP applications with ease.
Official Documentation
You can find the official documentation for ZenithPHP at ZenithPHP Documentation. The documentation provides detailed information on how to get started with ZenithPHP, including installation instructions, routing, controllers, models, views, and more.
Features
- MVC Architecture: Keeps code organized by separating the business logic, presentation, and data layers.
- Custom Routing: Define clean and flexible routes for your application with ease.
- Simple Namespacing: Eliminate the need for
require_oncewith proper namespacing. - Security Built-in: Features like password hashing and input validation to keep your app secure.
- Easy to Extend: Customize the framework to suit your application’s needs with minimal effort.
- Pluto Template Engine: Simplifies view rendering with a clean syntax and powerful directives.
Installation
To get started with ZenithPHP, follow the instructions below:
-
Clone the repository:
git clone https://github.com/ZenithPHP-Framework/full-zenith-framework.git
-
Navigate to the project directory:
cd full-zenith-framework -
Install dependencies using Composer:
composer install
-
Configure your environment:
- Create a copy of the
.env.examplefile and rename it to.env. - Set your database credentials and other necessary configurations.
- Create a copy of the
-
Start your local development server:
php cli run
-
Visit
http://localhost:8000in your browser to see the landing page.
Folder Structure
- App/: Contains the core application files.
- Config/: Configuration files like CORS for the application.
- Core/: The framework's core classes and functions.
- View/: Contains all view files (HTML/PHP templates).
- Public/: The main entry point for the application and assets (JS, CSS, images).
- .env: Environment variables for the application.
- .env.example: Example environment variables file.
- composer.json: Composer dependencies file.
- cli: Command-line interface for running the application and creating models and controllers.
How to Get Started
To create your first route:
-
Open
App/routes.phpand define your route:use ZenithPHP\Core\Http\Router; Router::get('/hello', 'WelcomeController', 'index');
-
Create a new controller (WelcomeController.php) inside
App/Controllers/:use ZenithPHP\Core\Controller\Controller; class WelcomeController extends Controller { public function index() { $this->view('welcome'); } }
-
Now, create a new view file inside
View/welcome.php:<h1>Welcome to ZenithPHP</h1>
-
Visit
http://localhost:8000/welcometo see the result.
Working with APIs
Now you can create APIs with ZenithPHP. Here's how you can create a simple API:
-
First, create a new route in
App/routes.php:use ZenithPHP\Core\Http\Router; Router::get('/api/users', 'UserController', 'index');
-
Create a new model using the CLI tool:
php cli make:model User
-
Implement the model method to fetch data from the database:
use ZenithPHP\Core\Model\Model; class User extends Model { protected string $table_name = 'users'; }
-
Create a new controller. For that, you can use the CLI tool:
php cli make:controller UserController
-
Implement the controller method to return JSON data:
use ZenithPHP\Core\Controller\Controller; use ZenithPHP\Core\Http\Response; use ZenithPHP\Core\Http\Request; use ZenithPHP\App\Models\User; class UserController extends Controller { public function index(Request $request, Response $response) { $user = new User($this->pdo); $allUsers = $user->getAll(); $response->json(['status' => 'success', 'data' => $allUsers]); } }
-
Visit
http://localhost:8000/api/usersto see the JSON response.
Pluto Template Engine
The Pluto template engine in ZenithPHP allows you to easily build dynamic views with clean and readable syntax. Here’s a quick overview of its directives:
Pluto Syntax
- Variable Output: Use
<< $variable >>to output PHP variables in the view. - Conditional Statements: Pluto offers
@if,@elseif,@else, and@endifdirectives for conditional rendering. - Looping: Use
@foreachand@endforeachto iterate over collections. - Comments: Use
<< // >>to add comments in your templates. - Yielding Sections: Use
@yieldto define a section that will be replaced with content from child templates. - Section Definition: Use
@sectionto define a section that can be filled in child templates.
Example Syntax
HomeController.php
class HomeController extends Controller { public function index(Request $request, Response $response) { return view('home', [ 'username' => 'John Doe', 'userRole' => 'admin', 'items' => [ (object) ['name' => 'Laptop', 'price' => '$999'], (object) ['name' => 'Smartphone', 'price' => '$499'], ] ]); } }
home.pluto.php
<!-- Displaying a variable --> << $username >> <!-- Conditional Statement --> @if ($userRole == 'admin') <p>Welcome, Admin!</p> @elseif ($userRole == 'member') <p>Welcome, Member!</p> @else <p>Welcome, Guest!</p> @endif <!-- Looping Through Data --> @foreach ($items as $item) <p><< $item->name >> - << $item->price >></p> @endforeach
Security Features
- Password Hashing: Built-in password hashing methods for user authentication.
- Input Validation: Functions to validate and sanitize user inputs.
Contributing
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature/bugfix.
- Submit a pull request with a clear description of your changes.
License
ZenithPHP is open-source and licensed under the MIT License. See the LICENSE file for more details.
Built with ❤️ by Dasun Nethsara