1. Go to this page and download the library: Download connora/basic library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
connora / basic example snippets
// 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)
]);
}
$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']);
});
/**
* Return an array of all sanitized inputs from the request
*/
$request->all();
/**
* Return the sanitized $_GET value by it's key, optional default value
*/
$request->get(string $key, string $default = null);
/**
* Return the sanitized $_POST value by it's key, optional default value
*/
$request->post(string $key, string $default = null);
/**
* Return the sanitized $_REQUEST value by it's key, optional default value
*/
$request->input(string $key, string $default = null);
namespace App\Controllers;
use App\Core\Request;
class ExampleController
{
public function update()
{
// standard instantiation
$request = new Request();
$data = $request->input('data');
// or with the helper
$data = request()->input('data');
}
}
namespace App\Controllers;
use App\Core\View;
use App\Models\UserModel;
class UserController
{
private $userModel;
// utilizing the containers automatic resolution
// by type hinting the class we want
public function __construct(UserModel $userModel)
{
$this->userModel = $userModel;
}
public function index()
{
$users = $this->userModel->getAll();
return View::render('pages.users.list', ['users' => $users]);
}
}
/**
* Establish any container class bindings for the application
*/
public function containerSetup(): self
{
// his->container->setOnce(Request::class, function ($container) {
return new Request();
});
$this->container->setOnce(DB::class, function ($container) {
$dbConfig = config('database', 'main');
return new DB(
$dbConfig['name'],
$dbConfig['username'],
$dbConfig['password'],
$dbConfig['host']
);
});
// reference the actual repository whenever the interface is referenced/injected
$this->container->set(UserRepositoryInterface::class, function ($container) {
return new UserRepository($container->get(UserModel::class));
});
return $this;
}
namespace App\Controllers;
use App\Core\View;
use App\Models\UserModel;
class ExampleController
{
public function index()
{
// get/resolve the UserModel class from the container without injecting into the constructor
// dependencies automatically resolved, pretty neat
$userModel = container(UserModel::class);
$user = $userModel->getById(request()->input('id'));
return View::render('pages.example', ['user' => $user]);
}
}
namespace App\Controllers;
use App\Core\View;
use App\Models\ExampleModel;
class ExampleController
{
private $exampleModel;
public function __construct(ExampleModel $exampleModel)
{
$this->exampleModel = $exampleModel;
}
public function index()
{
return View::render('pages.example', [
'data' => $this->exampleModel->getAll();
]);
}
}
namespace App\Controllers;
use App\Core\DB;
use App\Models\ExampleModel;
class ExampleController
{
private $db;
public function __construct(DB $db)
{
// Ex.1
// Use the main DB connection that is configured in the container
// via the type-hinted constructor argument
$this->db = $db;
// Ex.2
// Create an alternative DB class binding in the container
// Useful for models that need a different database connection
$this->db = container('db_alt');
// Ex.3
// Create a connection on the fly
$dbConfig = config('database', 'alt');
$this->db = new DB(
$dbConfig['name'],
$dbConfig['username'],
$dbConfig['password'],
$dbConfig['host']
);
}
public function index()
{
$exampleModel = new ExampleModel($this->db);
return View::render('pages.example', [
'data' => $exampleModel->getAll();
]);
}
}
namespace App\Models;
use App\Core\Model;
class UserModel extends Model
{
private $table = 'users';
public function getAll()
{
$sql = "SELECT * FROM $this->table";
return $this->db->query($sql);
}
public function getById($id)
{
$sql = "SELECT * FROM $this->table WHERE id = ?";
return $this->db->single($sql, [$id]);
}
public function getByEmail($email)
{
$sql = "SELECT * FROM $this->table WHERE email = ?";
return $this->db->single($sql, [$email]);
}
public function create($name, $email, $username, $password)
{
$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO $this->table(name, email, username, password)
VALUES(:name, :email, :username, :password)";
return $this->db->execute($sql, [
'name' => $name,
'email' => $email,
'username' => $username,
'password' => $hashedPwd,
]);
}
public function update(int $userId, array $properties)
{
$setString = '';
foreach ($properties as $property => $value) {
$setString .= $property . ' = ' . ':' . $property;
if ($property != array_key_last($properties)) {
$setString .= ', ';
} else {
$setString .= ' ';
}
}
$properties['id'] = $userId;
$sql = "UPDATE $this->table
SET $setString
WHERE id = :id";
return $this->db->execute($sql, $properties);
}
public function delete(int $userId)
{
$sql = "DELETE FROM $this->table WHERE id = ?";
return $this->db->execute($sql, [$userId]);
}
}
// get the main database connection host name
$host = config('database', 'main.host');
php
// 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']);
php
$this->router->get('/example/#id', function ($id) {
return $id
});
php
$this->router->view('/', 'pages.welcome');
php
public function index()
{
$foo = 'bar';
return View::render('pages.example', ['foo' => $foo]);
}
php
namespace App\Models;
use App\Core\Model;
/**
* $this->db available to use for your queries
*/
class ExampleModel extends Model
{
private $table = 'example';
public function getAll()
{
$sql = "SELECT * FROM $this->table";
return $this->db->query($sql);
}
}
php
/**
* Get the established PDO connection
*/
$this->db->pdo();
/**
* Prepares the query, binds the params, executes, and runs a fetchAll()
*/
$this->db->query(string $sql, array $params = []);
/**
* Prepares the query, binds the params, executes, and runs a fetch()
*/
$this->db->single(string $sql, array $params = []);
/**
* Prepares the query, binds the params, and executes the query
*/
$this->db->execute(string $sql, array $params = []);
bash command-line
php basic serve
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.