commit
e61576821e
7 changed files with 28 additions and 28 deletions
|
@ -8,9 +8,9 @@ To start, create an empty directory for your project. You also need an entry poi
|
||||||
|
|
||||||
A common way to do this is to just put the `index.php` in the root folder of the projects. This is also how some frameworks do it. Let me explain why you should not do this.
|
A common way to do this is to just put the `index.php` in the root folder of the projects. This is also how some frameworks do it. Let me explain why you should not do this.
|
||||||
|
|
||||||
The `index.php` is the starting point, so it has to be inside the webserver directory. Which means that the webserver has access to all subdirectories. If you set things up properly, you can still prevent it from accessing your subfolders where your application files are.
|
The `index.php` is the starting point, so it has to be inside the web server directory. This means that the web server has access to all subdirectories. If you set things up properly, you can still prevent it from accessing your subfolders where your application files are.
|
||||||
|
|
||||||
But sometimes things don't go according to plan. And if something goes wrong and your files are set up as explained above, your whole application source code could get exposed to visitors. I won't have to explain why this is not a good thing.
|
But sometimes things don't go according to plan. And if something goes wrong and your files are set up as above, your whole application source code could be exposed to visitors. I won't have to explain why this is not a good thing.
|
||||||
|
|
||||||
So instead of doing that, create a folder in your project folder called `public`. This is a good time to create an `src` folder for your application, also in the project root folder.
|
So instead of doing that, create a folder in your project folder called `public`. This is a good time to create an `src` folder for your application, also in the project root folder.
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ require '../src/Bootstrap.php';
|
||||||
|
|
||||||
The `Bootstrap.php` will be the file that wires your application together. We will get to it shortly.
|
The `Bootstrap.php` will be the file that wires your application together. We will get to it shortly.
|
||||||
|
|
||||||
The rest of the public folder is reserved for your public asset files (like javascript files and stylesheets).
|
The rest of the public folder is reserved for your public asset files (like JavaScript files and stylesheets).
|
||||||
|
|
||||||
Now navigate inside your `src` folder and create a new `Bootstrap.php` file with the following content:
|
Now navigate inside your `src` folder and create a new `Bootstrap.php` file with the following content:
|
||||||
|
|
||||||
|
@ -34,10 +34,10 @@ Now navigate inside your `src` folder and create a new `Bootstrap.php` file with
|
||||||
echo 'Hello World!';
|
echo 'Hello World!';
|
||||||
```
|
```
|
||||||
|
|
||||||
Now let's see if everything is set up correctly. Open up a console and navigate into your projects `public` folder. In there type `php -S localhost:8000` and press enter. This will start the built-in webserver and you can access your page in a browser with `http://localhost:8000`. You should now see the hello world message.
|
Now let's see if everything is set up correctly. Open up a console and navigate into your projects `public` folder. In there type `php -S localhost:8000` and press enter. This will start the built-in webserver and you can access your page in a browser with `http://localhost:8000`. You should now see the 'hello world' message.
|
||||||
|
|
||||||
If there is an error, go back and try to fix it. If you only see a blank page, check the console window where the server is running for errors.
|
If there is an error, go back and try to fix it. If you only see a blank page, check the console window where the server is running for errors.
|
||||||
|
|
||||||
Now would be a good time to commit your progress. If you are not already using git, set up a repository now. This is not a git tutorial so I won't go over the details. But using version control should be a habit, even if it is just for a tutorial project like this.
|
Now would be a good time to commit your progress. If you are not already using Git, set up a repository now. This is not a Git tutorial so I won't go over the details. But using version control should be a habit, even if it is just for a tutorial project like this.
|
||||||
|
|
||||||
[next >>](2-composer.md)
|
[next >>](2-composer.md)
|
|
@ -4,11 +4,11 @@
|
||||||
|
|
||||||
[Composer](https://getcomposer.org/) is a dependency manager for PHP.
|
[Composer](https://getcomposer.org/) is a dependency manager for PHP.
|
||||||
|
|
||||||
Just because you are not using a framework does not mean you will have to reinvent the wheel every time you want to do something. With composer you can install 3rd party libraries for your application.
|
Just because you are not using a framework does not mean you will have to reinvent the wheel every time you want to do something. With Composer, you can install third-party libraries for your application.
|
||||||
|
|
||||||
If you don't have composer installed already, head over to the website and install it. You can find composer packages for your project on [packagist](https://packagist.org/).
|
If you don't have Composer installed already, head over to the website and install it. You can find Composer packages for your project on [Packagist](https://packagist.org/).
|
||||||
|
|
||||||
Create a new file in your project root folder called `composer.json`. This is the composer configuration file that will be used to configure your project and it's dependencies. It must be valid json or composer will fail.
|
Create a new file in your project root folder called `composer.json`. This is the Composer configuration file that will be used to configure your project and its dependencies. It must be valid JSON or Composer will fail.
|
||||||
|
|
||||||
Add the following content to the file:
|
Add the following content to the file:
|
||||||
|
|
||||||
|
@ -40,14 +40,14 @@ In the autoload part you can see that I am using the `Example` namespace for the
|
||||||
|
|
||||||
Open a new console window and navigate into your project root folder. There run `composer update`.
|
Open a new console window and navigate into your project root folder. There run `composer update`.
|
||||||
|
|
||||||
Composer creates a `composer.lock` file that locks in your dependencies and a vendor directory. To remove those from your git repository, add a new file in your project root folder called `.gitignore` with the following content:
|
Composer creates a `composer.lock` file that locks in your dependencies and a vendor directory. To remove those from your Git repository, add a new file in your project root folder called `.gitignore` with the following content:
|
||||||
|
|
||||||
```
|
```
|
||||||
composer.lock
|
composer.lock
|
||||||
vendor/
|
vendor/
|
||||||
```
|
```
|
||||||
|
|
||||||
This will exclude the included file and folder from your commits. For which would be a good time now by the way.
|
This will exclude the included file and folder from your commits. For which now would be a good time, by the way.
|
||||||
|
|
||||||
Now you have successfully created an empty playground which you can use to set up your project.
|
Now you have successfully created an empty playground which you can use to set up your project.
|
||||||
|
|
||||||
|
|
|
@ -27,9 +27,9 @@ Now before you start adding the error handler code to the
|
||||||
|
|
||||||
**Important:** Never show any errors in your production environment. A stack trace or even just a simple error message can help someone to gain access to your system. Always show a user friendly error page instead and send an email to yourself, write to a log or something similar. So only you can see the errors in the production environment.
|
**Important:** Never show any errors in your production environment. A stack trace or even just a simple error message can help someone to gain access to your system. Always show a user friendly error page instead and send an email to yourself, write to a log or something similar. So only you can see the errors in the production environment.
|
||||||
|
|
||||||
For development that does not make sense though and you want a nice error page. So the solution is to have an environment switch in your code. For now you can just set it to `development`.
|
For development that does not make sense though -- you want a nice error page. The solution is to have an environment switch in your code. For now you can just set it to `development`.
|
||||||
|
|
||||||
Then after the error handler registration, throw an Exception to test if everything is working correctly. Your `Bootstrap.php` should now look similar to this:
|
Then after the error handler registration, throw an `Exception` to test if everything is working correctly. Your `Bootstrap.php` should now look similar to this:
|
||||||
|
|
||||||
```
|
```
|
||||||
<?php
|
<?php
|
||||||
|
@ -59,7 +59,7 @@ throw new \Exception;
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
You should now see a nice error page with the line highlighted where you throw the exception. If not, go back and debug until you get it working. Now would also be a good time for another commit.
|
You should now see a error page with the line highlighted where you throw the exception. If not, go back and debug until you get it working. Now would also be a good time for another commit.
|
||||||
|
|
||||||
[<< previous](2-composer.md) | [next >>](4-http.md)
|
[<< previous](2-composer.md) | [next >>](4-http.md)
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
PHP already has a few things built in to make working with HTTP easier. For example there are the [superglobals](http://php.net/manual/en/language.variables.superglobals.php) that contain the request information.
|
PHP already has a few things built in to make working with HTTP easier. For example there are the [superglobals](http://php.net/manual/en/language.variables.superglobals.php) that contain the request information.
|
||||||
|
|
||||||
These are good if you just want to get a small script up and running without much thought on maintenance. But if you want to write clean, maintanable, [SOLID](http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29) code, then you will want a class with a nice object oriented interface that you can use in your application.
|
These are good if you just want to get a small script up and running without much thought on maintenance. However, if you want to write clean, maintainable, [SOLID](http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29) code, then you will want a class with a nice object-oriented interface that you can use in your application.
|
||||||
|
|
||||||
Once again, you don't have to reinvent the wheel and just install a package. I decided to write my own [HTTP component](https://github.com/PatrickLouys/http) because I did not like the existing components, but you don't have to do the same.
|
Once again, you don't have to reinvent the wheel and just install a package. I decided to write my own [HTTP component](https://github.com/PatrickLouys/http) because I did not like the existing components, but you don't have to do the same.
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ echo $response->getContent();
|
||||||
|
|
||||||
This will send the response data to the browser. If you don't do this, nothing happens as the `Response` object only stores data. This is handled differently by most other HTTP components where the classes send data back to the browser as a side-effect, so keep that in mind if you use another component.
|
This will send the response data to the browser. If you don't do this, nothing happens as the `Response` object only stores data. This is handled differently by most other HTTP components where the classes send data back to the browser as a side-effect, so keep that in mind if you use another component.
|
||||||
|
|
||||||
Right now it is just sending an empty response back to the browser with the status code `200`, so to change that add the following code between the code snippets from above:
|
Right now it is just sending an empty response back to the browser with the status code `200`; to change that, add the following code between the code snippets from above:
|
||||||
|
|
||||||
```
|
```
|
||||||
$content = '<h1>Hello World</h1>';
|
$content = '<h1>Hello World</h1>';
|
||||||
|
@ -57,7 +57,7 @@ $response->setContent('404 - Page not found');
|
||||||
$response->setStatusCode(404);
|
$response->setStatusCode(404);
|
||||||
```
|
```
|
||||||
|
|
||||||
Remember that the object is only storing data, so you if you set multiple status codes before you send the response, the last one will be applied.
|
Remember that the object is only storing data, so you if you set multiple status codes before you send the response, only the last one will be applied.
|
||||||
|
|
||||||
I will show you in later parts how to use the different features of the components. In the meantime, feel free to read the [documentation](https://github.com/PatrickLouys/http) or the source code if you want to find out how something works.
|
I will show you in later parts how to use the different features of the components. In the meantime, feel free to read the [documentation](https://github.com/PatrickLouys/http) or the source code if you want to find out how something works.
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,9 @@ I will use [FastRoute](https://github.com/nikic/FastRoute) in this tutorial. But
|
||||||
|
|
||||||
Alternative packages: [symfony/Routing](https://github.com/symfony/Routing), [Aura.Router](https://github.com/auraphp/Aura.Router), [fuelphp/routing](https://github.com/fuelphp/routing), [Klein](https://github.com/chriso/klein.php)
|
Alternative packages: [symfony/Routing](https://github.com/symfony/Routing), [Aura.Router](https://github.com/auraphp/Aura.Router), [fuelphp/routing](https://github.com/fuelphp/routing), [Klein](https://github.com/chriso/klein.php)
|
||||||
|
|
||||||
By now you know how to install composer packages, so I will leave that to you.
|
By now you know how to install Composer packages, so I will leave that to you.
|
||||||
|
|
||||||
Now add this code block to your `Bootstrap.php` file where you added the hello world message in the last part.
|
Now add this code block to your `Bootstrap.php` file where you added the 'hello world' message in the last part.
|
||||||
|
|
||||||
```
|
```
|
||||||
$dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
|
$dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
|
||||||
|
|
|
@ -2,6 +2,6 @@
|
||||||
|
|
||||||
### Controllers
|
### Controllers
|
||||||
|
|
||||||
When I talk about a controller in this tutorial then I am just referring to a class that has handler methods. I am not talking about [MVC (Model-View-Controller)](http://martinfowler.com/eaaCatalog/modelViewController.html) controllers. MVC can't be implemented properly in PHP anyways, at least not in the way it was originally conceived. So forget about MVC and instead let's worry about [separation of concerns](http://en.wikipedia.org/wiki/Separation_of_concerns).
|
When I talk about a controller in this tutorial then I am just referring to a class that has handler methods. I am not talking about [MVC (Model-View-Controller)](http://martinfowler.com/eaaCatalog/modelViewController.html) controllers. MVC can't be implemented properly in PHP anyway, at least not in the way it was originally conceived. So forget about MVC and instead let's worry about [separation of concerns](http://en.wikipedia.org/wiki/Separation_of_concerns).
|
||||||
|
|
||||||
to be continued...
|
to be continued...
|
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
### Introduction
|
### Introduction
|
||||||
|
|
||||||
If you are really new to the language, this is not for you. This tutorial is aimed at people who have grasped the basics of PHP and know a little bit about object oriented programming.
|
If you are really new to the language, this is not for you. This tutorial is aimed at people who have grasped the basics of PHP and know a little bit about object-oriented programming.
|
||||||
|
|
||||||
You should at least heard of [SOLID](http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29). If you are not familiar with it, now would be a good time to familiarize yourself with the principles before you start with the tutorial.
|
You should at least heard of [SOLID](http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29). If you are not familiar with it, now would be a good time to familiarize yourself with the principles before you start with the tutorial.
|
||||||
|
|
||||||
I saw a lot of people coming into the stackoverflow PHP chatroom that asked if X framework is any good. Most of the time the answer was that they should just use PHP and not a framework to build their application. But many are overwhelmed by this and don't know where to start.
|
I saw a lot of people coming into the Stack Overflow PHP chatroom and asking if framework X is any good. Most of the time the answer was that they should just use PHP and not a framework to build their application. But many are overwhelmed by this and don't know where to start.
|
||||||
|
|
||||||
So my goal with this is to provide an easy resource that people can be pointed to. In most cases a framework does not make sense and writing an application from scratch with the help of some 3rd party packages is much, much easier than some people think.
|
So my goal with this is to provide an easy resource that people can be pointed to. In most cases a framework does not make sense and writing an application from scratch with the help of some third party packages is much, much easier than some people think.
|
||||||
|
|
||||||
So let's get started right away with the [first part](1-front-controller.md).
|
So let's get started right away with the [first part](1-front-controller.md).
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue