From 5e7186c67ace936181482fb1fa472b6e686d9fac Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 12 Sep 2014 23:21:50 +0200 Subject: [PATCH] refactored --- 1-setup.md => 1-front-controller.md | 55 ++--------------------------- 2-composer.md | 52 +++++++++++++++++++++++++++ 2-http.md => 3-error-handler.md | 10 ++---- 4-http.md | 9 +++++ README.md | 8 +++-- 5 files changed, 72 insertions(+), 62 deletions(-) rename 1-setup.md => 1-front-controller.md (54%) create mode 100644 2-composer.md rename 2-http.md => 3-error-handler.md (81%) create mode 100644 4-http.md diff --git a/1-setup.md b/1-front-controller.md similarity index 54% rename from 1-setup.md rename to 1-front-controller.md index 4a1174a..6b1f446 100644 --- a/1-setup.md +++ b/1-front-controller.md @@ -1,6 +1,6 @@ -[next >>](2-http.md) +[next >>](2-composer.md) -### Setting up the front controller +### 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. @@ -40,53 +40,4 @@ Now would be a good time to commit your progress. If you are not already using g What you have set up now is called a [front controller](http://en.wikipedia.org/wiki/Front_Controller_pattern). All requests are going to the same file that then decides what to do with it. This is a very common software design pattern. -### Introducing composer - -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. For PHP there is a dependency manager called composer that you can use to pull in 3rd party packages for your application. - -If you don't have composer installed already, head over to the [website](https://getcomposer.org/) 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. - -Add the following content to the file: - -``` -{ - "name": "Project name", - "description": "Your project description", - "keywords": ["Your keyword", "Another keyword"], - "license": "MIT", - "authors": [ - { - "name": "Your Name", - "email": "your@email.com", - "role": "Creator / Main Developer" - } - ], - "require": { - "php": ">=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. - -Now you have successfully created an empty playground which you can use to set up your project. - -[next >>](2-http.md) \ No newline at end of file +[next >>](2-composer.md) \ No newline at end of file diff --git a/2-composer.md b/2-composer.md new file mode 100644 index 0000000..2695755 --- /dev/null +++ b/2-composer.md @@ -0,0 +1,52 @@ +[<< previous](1-front-controller.md) | [next >>](3-error-handler.md) + +### Composer + +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. For PHP there is a dependency manager called composer that you can use to pull in 3rd party packages for your application. + +If you don't have composer installed already, head over to the [website](https://getcomposer.org/) 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. + +Add the following content to the file: + +``` +{ + "name": "Project name", + "description": "Your project description", + "keywords": ["Your keyword", "Another keyword"], + "license": "MIT", + "authors": [ + { + "name": "Your Name", + "email": "your@email.com", + "role": "Creator / Main Developer" + } + ], + "require": { + "php": ">=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. + +Now you have successfully created an empty playground which you can use to set up your project. + +[<< previous](1-front-controller.md) | [next >>](3-error-handler.md) \ No newline at end of file diff --git a/2-http.md b/3-error-handler.md similarity index 81% rename from 2-http.md rename to 3-error-handler.md index 0dc009a..2738b8c 100644 --- a/2-http.md +++ b/3-error-handler.md @@ -1,6 +1,6 @@ -[<< previous](1-setup.md) | [next >>](3-.md) +[<< previous](2-composer.md) | [next >>](4-http.md) -### Error handling +### Error Handler The first package for the project will be an error handler that will provide you with nice errors. A nice error page with a lot of information for debugging goes a long way during development. @@ -59,9 +59,5 @@ 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. -### HTTP abstractions - -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 quick and dirty script runnin. 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. +[<< previous](2-composer.md) | [next >>](4-http.md) diff --git a/4-http.md b/4-http.md new file mode 100644 index 0000000..bdda196 --- /dev/null +++ b/4-http.md @@ -0,0 +1,9 @@ +[<< previous](3-error-handler.md) + +### HTTP + +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 quick and dirty script runnin. 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. + +to be continued... \ No newline at end of file diff --git a/README.md b/README.md index c80d3f4..c3fead3 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,11 @@ I saw a lot of people coming into the stackoverflow PHP chatroom that asked if X 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 with the [setup](1-setup.md). +So let's get started right away with the [first part](1-front-controller.md). ### Parts -* [1. Setting up the project](1-setup.md). -* [2. HTTP abstraction](2-http.md). \ No newline at end of file +1. [Front Controller](1-front-controller.md). +2. [Composer](2-composer.md). +3. [Error Handler](3-error-handler.md). +4. [HTTP](4-http.md). \ No newline at end of file