no-framework-tutorial/2-composer.md

55 lines
2.1 KiB
Markdown
Raw Normal View History

2014-09-12 21:21:50 +00:00
[<< previous](1-front-controller.md) | [next >>](3-error-handler.md)
### Composer
2014-09-14 18:10:37 +00:00
[Composer](https://getcomposer.org/) is a dependency manager for PHP.
2014-09-12 21:21:50 +00:00
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.
2014-09-14 18:10:37 +00:00
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/).
2014-09-12 21:21:50 +00:00
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.
2014-09-12 21:21:50 +00:00
Add the following content to the file:
2014-09-17 20:07:26 +00:00
```json
2014-09-12 21:21:50 +00:00
{
"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:
2014-09-12 21:21:50 +00:00
2014-09-17 20:07:26 +00:00
```php
2014-09-12 21:21:50 +00:00
composer.lock
vendor/
```
This will exclude the included file and folder from your commits. For which now would be a good time, by the way.
2014-09-12 21:21:50 +00:00
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)