![PHP Tutorial: How to make your own PHP framework [part 2 of 2]](https://artslap.io/wp-content/uploads/2022/08/1200x628_elasticsearch-tutorial-min.jpg)
PHP Tutorial: How to make your own PHP framework [part 2 of 2]
This is the second part in our “How to make your own PHP framework”-tutorial. Check out the first part of […]
Read moreWe’ve all been there; “how does a framework work?”, “what is the purpose of a framework?” or maybe even “how do I make my own framework?”.
In this tutorial we’ll teach you to make your own PHP framework in the simplest way possible. The things we’ll be learning are:
Application
classBefore starting however, it might be useful to have a server like Wamp installed so you can locally host a webserver.
To start building the framework, we’ll need a class
that handles the startup and load of the things we need for the framework.
We can begin by creating an index.php
and adding the following code:
<?php
class Application
{
public function __construct()
{
$this->request = new Request();
$this->routes = [];
}
}
Very important is to note the beginning of the file: <?php
, this marks the beginning of PHP code.
This is Application
class for our framework. The __construct
method is called every time a new instance of Application
is created. You might be saying; “But wait, what’s Request
?” and you’re absolutely right. .
In the same index.php
file, after the Application
, add the following Request
-class:
class Request
{
public function __construct() {
$this->url = parse_url($_SERVER['REQUEST_URI']);
$this->queryParams = $_GET;
if ($_POST) {
$this->postParams = $_POST;
}
if ($_FILES) {
$this->files = $_FILES;
}
}
}
That’s a lot of code right? This __construct
method is a lot larger than the Application
‘s. That’s because we’re already (sort of) loading the requested URL in our Request
class. That means every time we create a new Request
, the current url
, query
-parameters, post
-body and even the uploaded files
are added to this class.
Next, we’re going to add some more methods
to our Request
class. Place the method
‘s below anywhere in the Request
class and your Request
class should look something like this.
class Request
{
public function __construct() {
...
}
public function getPath()
{
return $this->url["path"];
}
public function getQueryParams()
{
return $this->queryParams;
}
public function getPostParams()
{
return $this->postParams;
}
public function getFiles()
{
return $this->files;
}
}
It is recommended to put any “local methods” below the __construct
method. This helps organize your code so you always know exactly where to look.
At the bottom of your index.php
file, outside of any classes
, add the following code:
$app = new Application();
$app->addRoutes([
"/my-page" => function (Request $request) {
return $this->render("templates/my-page.php", [
"title" => "I made this myself!",
"content" => "Hello from my page!"
]);
}
]);
$app->run();
The code above does the following:
Application
and name it $app
$app
, we call the method addRoutes
with an array
of arguments. /my-page
is the URL you would like to use and the following function (closure
) returns a rendered result.$app->run
method, to run the applicationBefore we conclude the first part of this tutorials, let’s add two more function
s to our Application
class named addRoutes
and run
:
class Application
{
public function __construct()
{
...
}
public function addRoutes()
{
}
public function run()
{
echo "Hoorah! We finished the first part of the tutorial!";
}
}
If you run the code (or visit your webserver) you should see the following message:
Hoorah! We finished the first part of the tutorial!
In the next post, we’ll structure our Application
, add route
-handling and even render some custom templates
.
Check out part two here: PHP Tutorial: How to make your own PHP framework [part 2 of 2]
This is the second part in our “How to make your own PHP framework”-tutorial. Check out the first part of […]
Read moreThis is a post about if statements and how to use them accordingly (depending on your language of course).
Read more
Somebody essentially lend a hand to make severely posts I would state. That is the very first time I frequented your web page and so far? I amazed with the research you made to make this particular submit incredible. Excellent task!
I am extremely impressed together with your writing talents as smartly as with the layout in your blog. Is that this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it抯 rare to peer a nice weblog like this one today..
I抎 have to verify with you here. Which is not one thing I normally do! I enjoy reading a put up that may make folks think. Also, thanks for allowing me to comment!