Skip to content

Commit

Permalink
Use PSR 4
Browse files Browse the repository at this point in the history
  • Loading branch information
buihanh2304 committed Jun 30, 2023
1 parent 4b8da33 commit 6eaf795
Show file tree
Hide file tree
Showing 32 changed files with 284 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace App\Controllers;

use System\Classes\Controller;

class HomeController extends Controller
{
public function index()
Expand Down
31 changes: 20 additions & 11 deletions app/controllers/User.php → app/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace App\Controllers;

use App\Libraries\UserLibrary;
use App\Models\User;
use System\Classes\Captcha;
use System\Classes\Container;
use System\Classes\Controller;

class UserController extends Controller
{
private UserModel $userModel;
private User $userModel;
private UserLibrary $userLibrary;

function __construct()
{
parent::__construct();
$this->userModel = $this->load->model('User');

$this->userModel = new User;
$this->userLibrary = new UserLibrary;
}

public function logout()
Expand All @@ -31,7 +42,6 @@ public function login()
redirect('/');
}

$userLibrary = $this->load->library('User');
$error = false;
$email = $this->request->postVar('email', '');
$password = $this->request->postVar('password', '');
Expand All @@ -45,15 +55,15 @@ public function login()

switch ($type) {
case 'email':
$error = $userLibrary->validateEmail($email);
$error = $this->userLibrary->validateEmail($email);
break;

default:
$error = $userLibrary->validateAccount($email);
$error = $this->userLibrary->validateAccount($email);
}

if (!$error) {
$error = $userLibrary->validatePassword($password);
$error = $this->userLibrary->validatePassword($password);
}

if (!$error) {
Expand Down Expand Up @@ -99,30 +109,29 @@ public function register()
$email = $this->request->postVar('email', '');

if ($this->request->getMethod() === 'POST') {
$userLibrary = $this->load->library('User');
// check account
$check = $userLibrary->validateAccount($account);
$check = $this->userLibrary->validateAccount($account);

if ($check) {
$error[] = $check;
}

// check password
$check = $userLibrary->validatePassword($password);
$check = $this->userLibrary->validatePassword($password);

if ($check) {
$error[] = $check;
}

// check repeat password
$check = $userLibrary->validatePasswordConfirmation($password, $re_password);
$check = $this->userLibrary->validatePasswordConfirmation($password, $re_password);

if ($check) {
$error[] = $check;
}

// check email
$check = $userLibrary->validateEmail($email);
$check = $this->userLibrary->validateEmail($email);

if ($check) {
$error[] = $check;
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions app/libraries/User.php → app/Libraries/UserLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace App\Libraries;

class UserLibrary
{
public function validateEmail($email)
Expand Down
6 changes: 5 additions & 1 deletion app/models/User.php → app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

class UserModel extends Model
namespace App\Models;

use System\Classes\Model;

class User extends Model
{
public function logout()
{
Expand Down
25 changes: 25 additions & 0 deletions app/Services/RouteService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Services;

use System\Classes\Container;
use System\Classes\Router;
use System\Interfaces\ServiceInterface;

class RouteService implements ServiceInterface
{
protected $namespace = 'App\\Controllers\\';

public function register()
{
$router = Container::get(Router::class);
$router->setNamespace($this->namespace);

$this->loadRoutes();
}

protected function loadRoutes()
{
require_once ROOT . 'app' . DS . 'routes.php';
}
}
40 changes: 40 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
// This file is a part of K-MVC
// version: 1.1.0
// author: MrKen
// website: https://vdevs.net
// github: https://github.com/buihanh2304/simple-php-mvc-framework
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

use App\Controllers\HomeController;
use System\Classes\Container;
use System\Classes\Router;
use System\Classes\Template;

/** @var Router */
$router = Container::get(Router::class);

$router->add('/', 'HomeController@index');

$router->add('/home', function () {
/** @var Template */
$view = Container::get(Template::class);

return $view->render('home/main');
});

$router->add('/test/1', [HomeController::class, 'index']);
$router->add('/test/2', [HomeController::class, 'index'], ['get']);
$router->add('/api/test', function () {
return [
'response' => 'ok',
];
});

// exemple
$router->add('/register', 'UserController@register', 'GET|POST');
$router->add('/#', 'UserController@login', 'GET|POST');
$router->add('/logout', 'UserController@logout', 'GET|POST');
Empty file removed app/services/.gitkeep
Empty file.
15 changes: 14 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
{
"name": "buihanh2304/simple-php-mvc-framework",
"description": "A simple PHP MVC Framework",
"keywords": ["php", "mvc", "framework"],
"keywords": [
"php",
"mvc",
"framework"
],
"license": "MIT",
"config": {
"sort-packages": true
},
"require": {
"php": "^8.0",
"league/plates": "3.*"
},
"autoload": {
"files": [
"system/functions.php"
],
"psr-4": {
"App\\": "app/",
"System\\": "system/"
}
}
}
32 changes: 0 additions & 32 deletions configs/autoload/routes.php

This file was deleted.

4 changes: 4 additions & 0 deletions configs/autoload/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

use App\Services\RouteService;
use System\Services\CaptchaService;

return [
RouteService::class,
CaptchaService::class,
];
2 changes: 0 additions & 2 deletions configs/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

// default controller
define('DEFAULT_CONTROLLER', 'Home');
// URL
define('SITE_SCHEME', 'http://');
define('SITE_HOST', 'basic.pro');
Expand Down
11 changes: 8 additions & 3 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
define('_MVC_START', microtime(true));

/*
// This file is a part of K-MVC
Expand All @@ -10,9 +9,15 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

use System\Classes\Container;
use System\Classes\Kernel;
use System\Classes\Request;

define('_MVC_START', microtime(true));

require('../system/bootstrap.php');

/** @var Kernel */
$kernel = Container::get('Kernel');
$kernel = Container::get(Kernel::class);

$kernel->run(Container::get('Request'));
$kernel->run(Container::get(Request::class));
25 changes: 14 additions & 11 deletions system/classes/Auth.php → system/Classes/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Auth
{
public $id = 0;
public $rights = 0;
public $id = 0;
public $rights = 0;
public $isLogin = false;
public $user = [
'id' => 0,
'account' => '',
'password' => '',
'email' => '',
'join_date' => '',
'rights' => 0,
'last_login' => 0,
'name' => '',
'id' => 0,
'account' => '',
'password' => '',
'email' => '',
'join_date' => '',
'rights' => 0,
'last_login' => 0,
'name' => '',
];

public $settings;
Expand All @@ -31,7 +33,8 @@ class Auth

public function __construct()
{
$this->db = Container::get('DB');
$this->db = Container::get(DB::class);

$this->authorize();
}

Expand Down
4 changes: 3 additions & 1 deletion system/classes/Captcha.php → system/Classes/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Captcha
{
private $font = 'monofont.ttf';
Expand Down Expand Up @@ -50,7 +52,7 @@ protected function generateCode()

public function check($name = 'captcha')
{
$request = Container::get('Request');
$request = Container::get(Request::class);
$code = isset($_SESSION['code']) ? trim($_SESSION['code']) : '';
$captcha = $request->postVar($name, '');

Expand Down
2 changes: 2 additions & 0 deletions system/classes/Config.php → system/Classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Config
{
private $configs;
Expand Down
2 changes: 2 additions & 0 deletions system/classes/Container.php → system/Classes/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Container
{
private static $instances = [];
Expand Down
Loading

0 comments on commit 6eaf795

Please # to comment.