PHPackages                             sleekphp/sleekphp - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. sleekphp/sleekphp

ActiveProject[Framework](/categories/framework)

sleekphp/sleekphp
=================

A lightweight beginner-friendly PHP framework using MVC and a simple ORM

v1.0.1(10mo ago)13MITPHPPHP &gt;=7.4

Since Jul 19Pushed 10mo agoCompare

[ Source](https://github.com/sleekphp/sleekphp)[ Packagist](https://packagist.org/packages/sleekphp/sleekphp)[ RSS](/packages/sleekphp-sleekphp/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

sleekPHP Framework Documentation
================================

[](#sleekphp-framework-documentation)

**sleekPHP** is a lightweight, beginner-friendly PHP framework inspired by Laravel, designed to simplify web development with an MVC structure, basic ORM, and migration capabilities.

---

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Folder Structure](#folder-structure)
- [Core Components](#core-components)
- [Creating Routes](#creating-routes)
- [Creating Controllers](#creating-controllers)
- [Using Views](#using-views)
- [Command Line Interface (CLI)](#command-line-interface-cli)
- [Using Models and ORM](#using-models-and-orm)
- [Database Migrations](#database-migrations)
- [Running the Development Server](#running-the-development-server)
- [Environment Configuration](#environment-configuration)
- [License](#license)

---

Installation
------------

[](#installation)

1. Clone or download the **sleekPHP** repository.
2. Navigate to the project root directory and install dependencies using Composer:

    ```
    composer install
    ```
3. Run the following command to set up autoloading for the framework:

    ```
    composer dump-autoload
    ```
4. Start a local development server (details provided in [Running the Development Server](#running-the-development-server)).

---

Folder Structure
----------------

[](#folder-structure)

Here’s the basic structure of the **sleekPHP** framework:

```
sleekPHP/
├── app/
│   ├── Controllers/      # Application controllers
│   ├── Models/           # Application models (e.g., User.php)
│   └── Views/            # View files (.php)
├── config/               # Configuration files
├── database/             # Database migrations and seeds
│   ├── migrations/       # Stores migration files
├── public/               # Public files, including index.php
├── routes/               # Route files for web and API
├── storage/              # Cache and logs
├── system/               # Core framework files
│   ├── Core/             # Core components for MVC and ORM
│   ├── Database/         # Handles Schema and Blueprint for migrations
│   └── Console/          # CLI-related functionality
├── .env                  # Environment configuration file
├── composer.json         # Composer dependencies
└── README.md             # Documentation

```

---

Core Components
---------------

[](#core-components)

### 1. Router

[](#1-router)

The router handles HTTP requests and directs them to the appropriate controllers. Define routes in the `routes/web.php` and `routes/api.php` files.

### 2. Controller

[](#2-controller)

Controllers are located in `app/Controllers`. Each controller is responsible for handling requests and returning a response, usually by rendering a view.

### 3. View Engine

[](#3-view-engine)

The view engine is designed to parse custom directives (`@if`, `@foreach`, `@include`) in `.php` files. Views are stored in `app/Views`.

### 4. ORM and Model

[](#4-orm-and-model)

The base `Model` class in `system/Core/Model.php` provides a simple ORM, enabling basic CRUD operations, along with querying methods.

### 5. Database Migrations

[](#5-database-migrations)

The `Schema` and `Blueprint` classes in `system/Database` provide migration capabilities, allowing you to create and drop tables.

---

Creating Routes
---------------

[](#creating-routes)

Define routes in the `routes/web.php` file. Routes can be mapped to controller methods:

```
// routes/web.php

use System\Core\Router;

$router = new Router();

$router->get('/home', 'HomeController@index');
```

- **GET** and **POST** routes are supported using `get()` and `post()` methods.
- Use `dispatch()` in `public/index.php` to handle routing based on the URL and request method.

---

Creating Controllers
--------------------

[](#creating-controllers)

Controllers are PHP classes located in `app/Controllers`. Each method in the controller can be mapped to a route.

```
// app/Controllers/HomeController.php

namespace App\Controllers;

use System\Core\View;

class HomeController {
    public function index() {
        $data = ['name' => 'John Doe', 'tasks' => ['Task 1', 'Task 2']];
        return View::make('home', $data);
    }
}
```

In this example, `HomeController` has an `index` method that loads the `home.php` view with data.

---

Using Views
-----------

[](#using-views)

View files are stored in `app/Views` and use the `.php` extension. The view engine supports:

- **Variables** with `{{ $variable }}` syntax.
- **Conditional Statements** with `@if`, `@elseif`, `@else`, and `@endif`.
- **Loops** with `@foreach` and `@endforeach`.
- **Includes** with `@include('viewName')`.

Example view file:

```

Welcome, {{ $name }}

@if ($loggedIn)
    You are logged in!
@else
    Please log in.
@endif

Your Tasks
@foreach ($tasks as $task)
    {{ $task }}
@endforeach
```

---

Command Line Interface (CLI)
----------------------------

[](#command-line-interface-cli)

The `sleek` command file provides several commands for easy project management.

1. **Make Controller**:

    ```
    php sleek make:controller ControllerName
    ```

    This command creates a new controller in `app/Controllers`.
2. **Make Model**:

    ```
    php sleek make:model ModelName
    ```

    This command creates a new model in `app/Models` with an ORM structure.
3. **Make View**:

    ```
    php sleek make:view ViewName
    ```

    This command creates a new view file in `app/Views`.
4. **Make Migration**:

    ```
    php sleek make:migration MigrationName
    ```

    Creates a new migration file in `database/migrations`.
5. **Run Migrations**:

    ```
    php sleek migrate
    ```

    Runs all migrations, executing each `up` method to apply database changes.
6. **Run Development Server**:

    ```
    php sleek run
    ```

    Starts a development server at `http://localhost:8000`.

---

Using Models and ORM
--------------------

[](#using-models-and-orm)

The base `Model` class provides basic CRUD operations and querying capabilities.

Example usage:

- **Creating a Record**:

    ```
    User::create([
        'name' => 'John Doe',
        'email' => 'john@example.com',
        'password' => password_hash('secret', PASSWORD_BCRYPT),
    ]);
    ```
- **Finding a Record by ID**:

    ```
    $user = User::find(1);
    echo $user->name;
    ```
- **Using Where Clauses**:

    ```
    $users = User::where('email', '=', 'john@example.com')->get();
    ```
- **First Record with Where**:

    ```
    $user = User::where('email', '=', 'john@example.com')->first();
    ```

---

Database Migrations
-------------------

[](#database-migrations)

Define migrations to create or modify tables in `database/migrations`.

Example migration to create a `users` table:

```
