First Commit
This commit is contained in:
Executable
+174
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use eftec\bladeone\BladeOne;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Monolog\Logger;
|
||||
use App\Models\CrudExample;
|
||||
use App\Services\Sanitizer;
|
||||
use App\Services\BaseController;
|
||||
|
||||
class CrudExampleController extends BaseController
|
||||
{
|
||||
protected $blade;
|
||||
protected $logger;
|
||||
|
||||
public function __construct(BladeOne $blade, Logger $logger)
|
||||
{
|
||||
$this->blade = $blade;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
protected function validateCsrfToken($token)
|
||||
{
|
||||
return hash_equals($_SESSION['csrf_token'], $token);
|
||||
}
|
||||
|
||||
public function index(Request $request): string
|
||||
{
|
||||
try {
|
||||
$crudexample = CrudExample::all();
|
||||
$this->logger->info('Displaying the CrudExampleController page.');
|
||||
|
||||
if ($crudexample->isEmpty()) {
|
||||
$this->logger->warning('No records found in the database.');
|
||||
// Continue with rendering the view instead of returning a message
|
||||
}
|
||||
|
||||
$crudexample = Sanitizer::sanitizeOutput($crudexample->toArray());
|
||||
|
||||
return $this->blade->run('crudexample.index', ['crudexample' => $crudexample]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error when querying the database: ' . $e->getMessage());
|
||||
return 'Error when querying the database: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function edit(Request $request, $id): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Editing record with ID: ' . $id);
|
||||
$crudexample = CrudExample::find($id);
|
||||
if (!$crudexample) {
|
||||
return 'Record not found';
|
||||
}
|
||||
|
||||
$csrfToken = generateCsrfToken();
|
||||
$crudexample = Sanitizer::sanitizeOutput($crudexample->toArray());
|
||||
|
||||
return $this->blade->run('crudexample.edit', ['crudexample' => $crudexample, 'csrfToken' => $csrfToken]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error when displaying the edit page: ' . $e->getMessage());
|
||||
return 'Error when displaying the edit page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function create(): string
|
||||
{
|
||||
try {
|
||||
return $this->blade->run('crudexample.create', ['error' => null]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error when displaying the create page: ' . $e->getMessage());
|
||||
return 'Error when displaying the create page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function store()
|
||||
{
|
||||
try {
|
||||
$data = $_POST;
|
||||
|
||||
if (!isset($data['csrf_token']) || !$this->validateCsrfToken($data['csrf_token'])) {
|
||||
throw new \Exception('Invalid CSRF token.');
|
||||
}
|
||||
|
||||
$data = Sanitizer::sanitizeInput($data);
|
||||
|
||||
if (!isset($data['password']) || empty($data['password'])) {
|
||||
throw new \Exception('The "password" field is required.');
|
||||
}
|
||||
|
||||
$data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
|
||||
|
||||
CrudExample::create($data);
|
||||
|
||||
header('Location: /crudexample');
|
||||
exit;
|
||||
} catch (\Exception $e) {
|
||||
if ($e->getCode() == 23000) { // Error code for duplicate
|
||||
$this->logger->error('Error when creating record: Duplicate email.');
|
||||
return $this->blade->run('crudexample.create', ['error' => 'This email already exists.']);
|
||||
} else {
|
||||
$this->logger->error('Error when creating record: ' . $e->getMessage());
|
||||
echo 'Error when creating record: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function update($id)
|
||||
{
|
||||
try {
|
||||
$data = $_POST;
|
||||
|
||||
if (!isset($data['csrf_token']) || !$this->validateCsrfToken($data['csrf_token'])) {
|
||||
throw new \Exception('Invalid CSRF token.');
|
||||
}
|
||||
|
||||
$data = Sanitizer::sanitizeInput($data);
|
||||
|
||||
if (isset($data['password']) && !empty($data['password'])) {
|
||||
$data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
|
||||
} else {
|
||||
unset($data['password']);
|
||||
}
|
||||
|
||||
$crudexample = CrudExample::find($id);
|
||||
if ($crudexample) {
|
||||
$crudexample->update($data);
|
||||
}
|
||||
|
||||
header('Location: /crudexample');
|
||||
exit;
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error when updating record: ' . $e->getMessage());
|
||||
echo 'Error when updating record: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function show(Request $request, $id): string
|
||||
{
|
||||
try {
|
||||
$crudexample = CrudExample::find($id);
|
||||
if (!$crudexample) {
|
||||
return 'Record not found';
|
||||
}
|
||||
|
||||
$crudexample = Sanitizer::sanitizeOutput($crudexample->toArray());
|
||||
|
||||
return $this->blade->run('crudexample.show', ['crudexample' => $crudexample]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error when displaying record details: ' . $e->getMessage());
|
||||
return 'Error when displaying record details: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(Request $request, $id)
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Deleting record with ID: ' . $id);
|
||||
$crudexample = CrudExample::find($id);
|
||||
if ($crudexample) {
|
||||
$crudexample->delete();
|
||||
}
|
||||
|
||||
header('Location: /crudexample');
|
||||
exit;
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error when deleting record: ' . $e->getMessage());
|
||||
echo 'Error when deleting record: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+127
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use eftec\bladeone\BladeOne;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Monolog\Logger;
|
||||
use App\Services\BaseController;
|
||||
|
||||
class HomeController extends BaseController
|
||||
{
|
||||
protected $blade;
|
||||
protected $logger;
|
||||
|
||||
public function __construct(BladeOne $blade, Logger $logger)
|
||||
{
|
||||
$this->blade = $blade;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function index(Request $request): string
|
||||
{
|
||||
$data = ['title' => 'Welcome to SysMVC'];
|
||||
return $this->blade->run('home', $data);
|
||||
}
|
||||
|
||||
public function about(): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the About page.');
|
||||
return $this->blade->run('home.about');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the About page: ' . $e->getMessage());
|
||||
return 'Error displaying the About page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function details(): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the Learn More page.');
|
||||
return $this->blade->run('home.details');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the Details page: ' . $e->getMessage());
|
||||
return 'Error displaying the Learn More page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function readme(): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the ReadMe page.');
|
||||
return $this->blade->run('home.readme');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the ReadMe page: ' . $e->getMessage());
|
||||
return 'Error displaying the ReadMe page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function database(): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the Database Usage page.');
|
||||
return $this->blade->run('home.database');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the Database Usage page: ' . $e->getMessage());
|
||||
return 'Error displaying the Database Usage page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function dependencyInjection(): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the Dependency Injection page.');
|
||||
return $this->blade->run('home.dependency-injection');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the Dependency Injection page: ' . $e->getMessage());
|
||||
return 'Error displaying the Dependency Injection page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function appDevelopment(): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the App Development page.');
|
||||
return $this->blade->run('home.app-development');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the App Development page: ' . $e->getMessage());
|
||||
return 'Error displaying the App Development page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
// New methods
|
||||
public function templatesBlade(): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the Blade Templates page.');
|
||||
return $this->blade->run('home.templates-blade');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the Blade Templates page: ' . $e->getMessage());
|
||||
return 'Error displaying the Blade Templates page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function security(): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the Security page.');
|
||||
return $this->blade->run('home.security');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the Security page: ' . $e->getMessage());
|
||||
return 'Error displaying the Security page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function consoleCli(): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the Console CLI page.');
|
||||
return $this->blade->run('home.console-cli');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the Console CLI page: ' . $e->getMessage());
|
||||
return 'Error displaying the Console CLI page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
use App\Services\BaseController;
|
||||
|
||||
use eftec\bladeone\BladeOne;
|
||||
|
||||
class IndexController extends BaseController
|
||||
{
|
||||
protected $blade;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// Configure BladeOne
|
||||
$this->blade = new BladeOne(VIEWS_PATH, CACHE_PATH, BladeOne::MODE_DEBUG);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
// Pass data to the view and render
|
||||
return $this->blade->run('home', ['title' => 'Welcome to SysMVC2025!']);
|
||||
}
|
||||
}
|
||||
Executable
+29
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use eftec\bladeone\BladeOne;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Monolog\Logger;
|
||||
use App\Services\BaseController;
|
||||
|
||||
class TesteController extends BaseController
|
||||
{
|
||||
protected $blade;
|
||||
protected $logger;
|
||||
|
||||
public function __construct(BladeOne $blade, Logger $logger)
|
||||
{
|
||||
$this->blade = $blade;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function index(Request $request): string
|
||||
{
|
||||
$data = ['title' => 'Bem-vindo ao TesteController'];
|
||||
$this->logger->info('Exibindo a página do TesteController.');
|
||||
return $this->blade->run('test', $data);
|
||||
}
|
||||
}
|
||||
|
||||
Executable
+184
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use eftec\bladeone\BladeOne;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Monolog\Logger;
|
||||
use App\Services\Sanitizer;
|
||||
use App\Services\BaseController;
|
||||
use App\Models\TwigExample;
|
||||
use Twig\Environment;
|
||||
|
||||
class TwigExampleController extends BaseController
|
||||
{
|
||||
protected $twig;
|
||||
protected $logger;
|
||||
protected $blade;
|
||||
|
||||
public function __construct(Environment $twig, Logger $logger, BladeOne $blade)
|
||||
{
|
||||
$this->twig = $twig;
|
||||
$this->logger = $logger;
|
||||
$this->blade = $blade;
|
||||
}
|
||||
|
||||
protected function validateCsrfToken($token)
|
||||
{
|
||||
return hash_equals($_SESSION['csrf_token'], $token);
|
||||
}
|
||||
|
||||
public function index(Request $request): string
|
||||
{
|
||||
try {
|
||||
$twigexample = TwigExample::all();
|
||||
$this->logger->info('Displaying the TwigExampleController page.');
|
||||
|
||||
if ($twigexample->isEmpty()) {
|
||||
$this->logger->warning('No records found in the database.');
|
||||
// Continue with rendering the view instead of returning a message
|
||||
}
|
||||
|
||||
$twigexample = Sanitizer::sanitizeOutput($twigexample->toArray());
|
||||
|
||||
return $this->twig->render('twigexample/index.twig.php', ['twigexample' => $twigexample]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error querying the database: ' . $e->getMessage());
|
||||
return 'Error querying the database: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function create(Request $request): string
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Displaying the creation page of the TwigExampleController.');
|
||||
return $this->twig->render('twigexample/create.twig.php', ['csrf_token' => htmlspecialchars(getCsrfToken(), ENT_QUOTES, 'UTF-8')]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the creation page: ' . $e->getMessage());
|
||||
return 'Error displaying the creation page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function edit(Request $request, $id): string
|
||||
{
|
||||
try {
|
||||
$twigexample = TwigExample::find($id);
|
||||
|
||||
if (!$twigexample) {
|
||||
$this->logger->warning("Record with ID $id not found.");
|
||||
return "Record with ID $id not found.";
|
||||
}
|
||||
|
||||
$twigexample = Sanitizer::sanitizeOutput($twigexample->toArray());
|
||||
|
||||
$this->logger->info("Displaying the edit page for the record with ID: $id.");
|
||||
return $this->twig->render('twigexample/edit.twig.php', ['twigexample' => $twigexample, 'csrf_token' => htmlspecialchars(getCsrfToken(), ENT_QUOTES, 'UTF-8')]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the edit page: ' . $e->getMessage());
|
||||
return 'Error displaying the edit page: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function show(Request $request, $id): string
|
||||
{
|
||||
try {
|
||||
$twigexample = TwigExample::find($id);
|
||||
|
||||
if (!$twigexample) {
|
||||
$this->logger->warning("Record with ID $id not found.");
|
||||
return "Record with ID $id not found.";
|
||||
}
|
||||
|
||||
$twigexample = Sanitizer::sanitizeOutput($twigexample->toArray());
|
||||
|
||||
$this->logger->info("Displaying the record with ID: $id.");
|
||||
return $this->twig->render('twigexample/show.twig.php', ['twigexample' => $twigexample]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error displaying the record: ' . $e->getMessage());
|
||||
return 'Error displaying the record: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(Request $request, $id)
|
||||
{
|
||||
try {
|
||||
$twigexample = TwigExample::find($id);
|
||||
|
||||
if (!$twigexample) {
|
||||
$this->logger->warning("Record with ID $id not found.");
|
||||
return "Record with ID $id not found.";
|
||||
}
|
||||
|
||||
$twigexample->delete();
|
||||
|
||||
$this->logger->info("Record with ID: $id deleted successfully.");
|
||||
header('Location: /twigexample');
|
||||
exit;
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error deleting the record: ' . $e->getMessage());
|
||||
return 'Error deleting the record: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function store()
|
||||
{
|
||||
try {
|
||||
$data = $_POST;
|
||||
|
||||
if (!isset($data['csrf_token']) || !$this->validateCsrfToken($data['csrf_token'])) {
|
||||
throw new \Exception('Invalid CSRF token.');
|
||||
}
|
||||
|
||||
$data = Sanitizer::sanitizeInput($data);
|
||||
|
||||
if (!isset($data['password']) || empty($data['password'])) {
|
||||
throw new \Exception('The "password" field is required.');
|
||||
}
|
||||
|
||||
$data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
|
||||
|
||||
TwigExample::create($data);
|
||||
|
||||
header('Location: /twigexample');
|
||||
exit;
|
||||
} catch (\Exception $e) {
|
||||
if ($e->getCode() == 23000) { // Error code for duplicate
|
||||
$this->logger->error('Error creating record: Duplicate email.');
|
||||
return $this->blade->run('twigexample.create', ['error' => 'This email already exists.']);
|
||||
} else {
|
||||
$this->logger->error('Error creating record: ' . $e->getMessage());
|
||||
echo 'Error creating record: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
try {
|
||||
$data = $_POST;
|
||||
|
||||
if (!isset($data['csrf_token']) || !$this->validateCsrfToken($data['csrf_token'])) {
|
||||
throw new \Exception('Invalid CSRF token.');
|
||||
}
|
||||
|
||||
$data = Sanitizer::sanitizeInput($data);
|
||||
|
||||
$twigexample = TwigExample::find($id);
|
||||
|
||||
if (!$twigexample) {
|
||||
$this->logger->warning("Record with ID $id not found.");
|
||||
return "Record with ID $id not found.";
|
||||
}
|
||||
|
||||
$twigexample->fill($data);
|
||||
$twigexample->save();
|
||||
|
||||
$this->logger->info("Record with ID: $id updated successfully.");
|
||||
header('Location: /twigexample');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error updating the record: ' . $e->getMessage());
|
||||
return 'Error updating the record: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user