Hello everyone! So this article will be on how you can create your own Docker registry hub and push your own Docker images to it. So before we start, here are the prerequisites. They're obvious, but let's make sure.
New changes to The Application
Posted on December 25, 2020697 views8 min read
So, there has been a lot of changes I made to The Application. These changes are major as I re-wrote the entire core files. I decided to do so because I felt like it was somehow incomplete. When I re-wrote the core files, I decided to go with using Autoloading. I felt it was the best approach and therefore yielded the most promise.
There are also a ton of changes to creating files and calling files now.
# Model
I have changed the way to create a model slightly differently. Instead of creating a file ending in .class.php, it will now be .model.php. This makes more sense because you're creating a model and it's also consistent to the naming convention. The model class will also be slightly different in the way you create the constructor. You will be passing the database connection and the Validate object into the constructor unlike before where you would just pass the database connection. The reason why we do this is that we may want to run a method within the Validate object, say for instance, the escape method. You can now also use return type declarations if you want. It is strongly recommended to do so as it'll make The Application more consistent.
To also access the model, you no longer will do something like this $this->model->testClass->testMethod(). Instead, you will be doing something like $this->model->testModel->testMethod(). They are relatively the same except instead of having Class, it's now Model. Everything else stays the same.
Here's an example of how to create a model file in this new update.
<?php
namespace The\Application\Model;
if(!defined('CHECK')) {
define('CHECK', true);
require_once '../core/Autoload.php';
new \The\Application\Rheta();
die;
}
use \PDO;
use \PDOException;
use \stdClass;
use \The\Application\Validate;
class TestModel {
public function __construct(object $db, Validate $validate) {
try {
$this->db = $db;
$this->validate = $validate;
} catch (PDOException $e) {
die('Database connection could not be established.');
}
}
public function testMethod(): array {
$sql = 'SELECT id, name FROM users';
$prepare = $this->db->prepare($sql);
$prepare->execute();
if($prepare->rowCount()) {
return $prepare->fetchAll();
} else {
return [];
}
}
}
# Controller
To create a controller file, you will still create the controller file within a folder that's the same name as your controller. For instance, say we want to create a controller file called test. We will create a folder called test and store our test.php file within that folder. So the structure may look something like /root/controller/test/test.php. Once we have properly stored our controller file, we now have to create a class that ends in the word controller. For instance, if we're creating a controller file called test, we now have to name our class like TestController. This will allow you to distinguish between files and allows you to know what that controller is for.
You will also have to give the index() method a proper type declaration and signature. The core Controller file implements from an interface and that interface requires you to have an index() method with the correct type declaration and signature.
So in example of our test controller would be something like this.
<?php
namespace The\Application;
if(!defined('CHECK')) {
define('CHECK', true);
require_once '../../core/Autoload.php';
new Rheta();
die;
}
class TestController extends Controller {
public function __construct() {
parent::__construct();
}
public function index(): void {
$testMethod = $this->model->testModel->testMethod();
if(empty($testMethod)) {
$this->call->controller('404', 'index');
} else {
$this->render->view('test', [
'passableVariable' => $testMethod
]);
}
}
}
# View
Creating a view is actually just the same as the old way. You create the view inside the view folder and within the specific folder you want to use. That's all. I don't think there really needs to be an introduction for that.
# Other things
So there are other new features I have added to this new update. I have added a feature where you can include a file into your controller without it being a part of The Application. For example, say you get a PHP class from somewhere and you don't know how to implement that file. Well, all you have to do is run the Load object to include that file. Then you just instantiate that class you want to use and that's it. So for instance, say we have a 3rd party class in our root folder and we want to use it. We simply just do something like this in our controller file.
<?php
namespace The\Application;
if(!defined('CHECK')) {
define('CHECK', true);
require_once '../../core/Autoload.php';
new Rheta();
die;
}
use \MyClass;
class TestController extends Controller {
public function __construct() {
parent::__construct();
}
public function index(): void {
$this->load->file(ROOT . 'myThirdpartyFile.php');
$myClass = new MyClass();
$myClass->hello();
}
}
And that's it. In our myThirdpartyFile.php file, it should have a method called hello() which should do something. The MyClass() would be the class from our myThirdpartyFile.php file. That's it. Basically, the Load object will try to find that file. If that file doesn't exist, it'll throw a fatal error. This makes it easy to use instead of trying to find those files yourself because it's already built into that Load object. It may seem a little bit redundant or useless, but it does come in handy when dealing with 3rd party files that you may not know how to implement directly into The Application.
Another addition to the new update that isn't really core file related is changing the style on the pre and code elements to reflect the scheme and themes I am using for my Sublime Text editor. I love the Bird of Paradise theme and now I have implemented it to the theme for code snippets. There is also line numbers now on the code snippets when you use Markdown. This helps viewers follow along with the code more easily now thanks to Prism.js. You can also now specify what language that code snippet is for by appending the language in lowercase after the 3 backticks (`). This will color code the code snippet to that specific language. This is pretty neat if you'd ask me.