PHPackages                             connora/basic - 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. connora/basic

ActiveProject[Framework](/categories/framework)

connora/basic
=============

A full-stack PHP framework that gives you the basics for starting a web project.

v2.1.2(1y ago)5282[1 PRs](https://github.com/connorabbas/basic-framework/pulls)MITPHPPHP &gt;=8.0

Since Aug 16Pushed 1y ago1 watchersCompare

[ Source](https://github.com/connorabbas/basic-framework)[ Packagist](https://packagist.org/packages/connora/basic)[ RSS](/packages/connora-basic/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (39)Used By (0)

Basic PHP Framework
===================

[](#basic-php-framework)

About
-----

[](#about)

A full-stack PHP framework that gives you the basics for starting a web project.

PHP 8 is required.

Key Features
------------

[](#key-features)

- MVC architecture
- Simple routing
- View templates using [Plates](https://platesphp.com/)
- Class auto loading and namespaces
- Dependency Injection Container
- PDO database wrapper class

---

Documentation
=============

[](#documentation)

- [Installation](#installation)
- [Routing](#routing)
- [Controllers](#controllers)
- [Dependency Injection Container](#dependency-injection-container)
- [Views](#views)
- [Models and Database](#models-and-database)
- [Helper Functions](#helper-functions)
- [Environmental and Configuration Data](#environmental-and-configuration-data)
- [CLI Tools](#cli-tools)

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

[](#installation)

Download using [Composer](https://getcomposer.org/).

```
composer create-project connora/basic your-project-name
```

The project `.env` file should be created on install when using composer. An `.env.example` file is included as well.

### Serving Your Site Locally

[](#serving-your-site-locally)

If you want to serve your site locally for quick testing or development and you have PHP installed on your machine, use the "serve" command while working in the root of your project.

> **Note:** this will only serve your site with php, not MySQL.

```
php basic serve
```

Alternatively, use Laragon, Docker, or XAMPP with a vhost configuration.

Routing
-------

[](#routing)

### Registering

[](#registering)

By default, you will register your routes within `/routes/main.php`.

You will have access to the `$this->router` property which supplies an instance of the `App\Core\Router` class.

The `App\Core\Router` class offers the following register methods that align with the common HTTP verbs:

```
$this->router->get($uri, $callback);
$this->router->post($uri, $callback);
$this->router->put($uri, $callback);
$this->router->patch($uri, $callback);
$this->router->delete($uri, $callback);
```

These methods will register your routes as valid endpoints within your application. If you try to access a url/route that isn't registered, a `404` page will be displayed.

### Callback Functions

[](#callback-functions)

The route's callback will either be a closure (an anonymous function) where you can execute your endpoint logic directly, or a reference to a controller method using an array, where the first item is the fully qualified class you want to reference, and the second item is the method name that will be called.

```
// Basic route using a closure
$this->router->get('/home', function () {
    return 'Hello World';
});
// Alternatively, use a controller class and a method to store your logic in
$this->router->get('/home-alt', [HomeController::class, 'index']);
```

### Dynamic Parameters

[](#dynamic-parameters)

You can set dynamic parameters in your routes uri by prefixing the slug with a hashtag `#`. The parameter's value will be available as an argument variable via your callback function.

Using within a controller:

```
// Ex: yoursite.com/blog/post/123
// within /routes/main.php
$this->router->get('/blog/post/#id', [BlogPostController::class, 'show']);

// within /app/controllers/BlogPostController.php
public function show($id)
{
    // $id = '123'
    return View::render('pages.blog.post.single', [
        'blogPostData' => $this->blogPostModel->findById($id)
    ]);
}
```

Or using a basic closure:

```
$this->router->get('/example/#id', function ($id) {
    return $id
});
```

### Batch Registering

[](#batch-registering)

You can register routes in batches that share similar qualities, such as a controller, a uri prefix, or both. The intent here is to reduce boilerplate code, and provide better organization.

When batching routes, it's required to chain on the `batch()` method at the end, which accepts a closure argument containing the routes you want the batch properties to apply to.

Batch related methods available to you:

```
$this->router->controller(string $className);
$this->router->prefixUri(string $uri);
$this->router->batch(callable $closure);
```

When batching routes with the `prefixUri()` method, the routes within the closure will all be prepended by your defined uri prefix.

For example:

```
$this->router
    ->prefixUri('/users')
    ->batch(function () {
        $this->router
            // /users GET (show all users)
            ->get('/', [UserController::class, 'index'])
            // /users/create GET (form to create a user)
            ->get('/create', [UserController::class, 'create'])
            // /users POST (endpoint to store a new user)
            ->post('/', [UserController::class, 'store'])
            // /users/123 GET (show a single user)
            ->get('/#id', [UserController::class, 'show'])
            // /users/123/edit GET (form to edit user properties)
            ->get('/#id/edit', [UserController::class, 'edit'])
            // /users/123 PATCH (endpoint to update user properties)
            ->patch('/#id', [UserController::class, 'update'])
            // /users/123 DELETE (endpoint to remove a user record)
            ->delete('/#id', [UserController::class, 'destroy']);
    });
```

When batching routes with the `controller()` method, take note that:

- The register method's second argument inside the closure will be a string referencing the endpoint method, instead of the default array syntax
- The `$this->router->view()` method is unavailable within the batch closure, since we are required to reference a controller method

For example:

```
$this->router
    ->controller(UserController::class)
    ->batch(function () {
        $this->router
            ->get('/users', 'index')
            ->get('/users/create', 'create')
            ->post('/users', 'store')
            ->get('/users/#id', 'show')
            ->get('/users/#id/edit', 'edit')
            ->patch('/users/#id', 'update')
            ->delete('/users/#id', 'destroy');
    });
```

Or use both:

```
$this->router
    ->controller(UserController::class)
    ->prefixUri('/users')
    ->batch(function () {
        $this->router
            ->get('/', 'index')
            ->get('/create', 'create')
            ->post('/', 'store')
            ->get('/#id', 'show')
            ->get('/#id/edit', 'edit')
            ->patch('/#id', 'update')
            ->delete('/#id', 'destroy');
    });
```

> **Note:** you cannot nest a `batch()` method within itself.

### Form Requests

[](#form-requests)

The standard html `` tag only accepts `GET` and `POST` as valid request methods. We can overcome this by using the `method_spoof(string $method)` helper function. This requires our form to use the `POST` method request and to specify the "spoofed" method inside the form using `PUT`, `PATCH`, or `DELETE`.

For example:

```
$this->router->patch('/update-example', [ExampleClass::class, 'updateMethod']);
```

```

    Field to update
