Installation
First of all, make sure you have installed PHP and Composer. Clone or download project and install dependencies (composer install).
composer install
First of all, make sure you have installed PHP and Composer. Clone or download project and install dependencies (composer install).
composer install
To create a route, in route/router.php, add your route within the rotas function, as shown in the example.
Use the $router
object, calling the method that corresponds to the HTTP verb you want to use.
As parameters, you should pass the route and the controller along with the method you want to call.
This framework uses the nikic/fast-route library.
Read its documentation for more information.
<?php
/*
|------------------------------------------------------------------------------------------------------
| Routes
|------------------------------------------------------------------------------------------------------
|
| Here is where you can register routes for your application.
| For more information see https://github.com/nikic/FastRoute
|
*/
function routes(FastRoute\RouteCollector $router): void
{
$router->get('/', 'App\Controllers\IndexController::index');
$router->get('/your-url', 'App\Controllers\YourController::yourMethod');
}
You must extend Core/Controller in your controller class as in the example on the side. The class and
method name (action) are the same you registered in router.php, and then, it will be called when
the route is accessed.
Your controller class will inherit the view()
method, which is responsible for rendering a
view. You can specify the view in the first parameter and pass data through the second parameter. Here is
the description of the view method parameters:
<?php
namespace App\Controllers;
use Core\Controller;
class UserController extends Controller {
public function signIn() {
$params = [
'user' => [
'name' => 'John Doe',
'email' => 'john.doe@domain.com',
]
];
$this->view('home', $params);
}
}
...
<body>
<span><?php echo $user["name"] ?></span>
<span><?php echo $user["email"] ?></span>
<\body>
...
John Doe
john.doe@domain.com
The Request class is a utility class that provides methods to access various information related to an
HTTP request. It is designed to be injected into a controller action (method) to handle request-related tasks.
That means all controllers have access to the Request class by parameters, therefore, just set the
parameter in your controller method and use it, like the example below.
Note: Remember to adjust the usage according to your specific application needs. The Request class
provides a convenient way to access request-related information within your controllers.
The Request class has the following methods:
$default
parameter defines a default value if the parameter is not found.
<?php
namespace App\Controllers;
use Core\Controller;
use Core\Request;
class MyController
{
public function handleRequest(Request $request)
{
// Example usage:
$method = $request->method();
$uri = $request->path();
$queryParams = $request->query();
$postParam = $request->body('postParam1', 'default');
// ... other logic
}
}
The Session class is a tool for managing PHP sessions. It encapsulates the session handling process in an object-oriented manner, providing a clean, intuitive and secure interface for session management. It follows the Singleton design pattern, meaning only one instance of this class can exist at a time. It provides methods to set and get session variables, set the session expiration time, and destroy the session.
You can set a session variable setting this variable in the session instance like an attribute. The same is
for recovering a session variable. For example, you can set a user session variable with
$session->user = $user
. See example below.
The Session class has the following methods:
$expire_time
parameter is optional and sets the session expiration time in seconds.
<?php
namespace App\Controllers;
use Core\Controller;
use Core\Session;
class AuthenticateController extends Controller
{
public function login()
{
// Get the Session instance
$session = Session::getInstance();
// Set a session variable
$session->set('user', 'John Doe');
$session->email = 'john.doe@example.com';
// ... continue with the logic
}
public function logout()
{
// Get the Session instance
$session = Session::getInstance();
// Destroy session
$session->destroy();
// continue...
}
}
<?php
namespace App\Controllers;
use Core\Controller;
use Core\Session;
class AnotherPageController extends Controller
{
public function show()
{
// Get the Session instance
$session = Session::getInstance();
// Get session variables
$username = $session->username;
$email = $session->get('email');
// ... continue with the logic
}
}
The Cookie class provides a secure and easy way to create, read, update and delete cookies. It abstracts the complexity of cookie handling and provides a clean and intuitive interface for managing cookies in PHP. See example below.
The Cookie class offers several methods for manipulating cookies. Let's take a look at each of them:
You must create a .env file at the root of your project. You can copy the .env.example file and set the environment variables as shown in the example below.
DRIVER=mysql
HOST=localhost
DBNAME=
DBUSER=
DBPASSWORD=
Done! Configs ready! Now we can create our Model and connect to the database.
In your model class, you can use Database class with use Core\Database;
.
It provides an interface for interacting with the database. This class follows the Singleton design pattern
to ensure that only one instance of the database connection is created throughout the application, thereby
saving resources.
This class is useful in Models as it provides a structured and reusable way to interact with the database. It abstracts the complexity of database operations, making it easier to perform CRUD (Create, Read, Update, Delete) operations.
Please note that the actual usage of this class may vary depending on the specific requirements of your application. Always ensure that you follow best practices for security and efficiency when interacting with a database.
Let’s look at some methods below:
<?php
namespace App\Models;
use Core\Database;
class User {
private table = 'users';
public function find($columns = '*', $conditions = null) {
$db = Database::getInstance();
$data = $db->getList($this->table, $columns, $conditions);
return $data;
}
}
<?php
namespace App\Controllers;
use Core\Controller;
use App\Models\User;
class UserController extends Controller {
public function getUser() {
$email = 'john.doe@domain.com';
$user = new User();
$targetUser = $user->find(null, 'email = ' . $email);
$this->view('home', ['user' => $targetUser]);
}
}