From 2d54bdd7aec84ff9f9a6c137aa3efbf530fe240e Mon Sep 17 00:00:00 2001 From: Patrick Louys Date: Fri, 12 Sep 2014 13:06:51 +0200 Subject: [PATCH] Update README.md --- README.md | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 494ab17..611441b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,96 @@ -no-framework-tutorial -===================== +## Creating a PHP application without a framework -A small tutorial to show how to create a PHP application without a framework. +### Why this 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. + +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 let's get started right away. + +### Setting up the front controller + +To start, create an empty directory for your project. You also need an entry point where all requests will go to. This means you will have to create an `index.php` file. + +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. + +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. + +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. + +Inside the `public` folder you can now create your `index.php`. Remember that you don't want to expose anything here, so put just the following code in there: + +``` +=5.5.0" + }, + "autoload": { + "psr-4": { + "Example\\": "src/" + } + } +} +``` + +In the autoload part you can see that I am using the `Example` namespace for the project. You can use whatever fits your project there, but from now on I will always use the `Example` namespace in my examples. Just replace it with your namespace in your own code. + +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.lock +vendor/ +``` + +This will exclude the included file and folder from your commits. For which would be a good time now by the way.