PHP Composer project standard directory layout - php

Is there any standard project structure for Composer projects such as the Maven's Standard Directory Layout in PHP?

The newest answer is "yes", based on scanning every package listed on https://packagist.org and counting which directories are being used by the vast majority of packages, i.e. what most people unconsciously agreed upon without coordination:
https://github.com/php-pds/skeleton
The short summary for directories:
If a package has a root-level directory for ...
... then it MUST be named:
command-line executables bin/
configuration files config/
documentation files docs/
web server files public/
other resource files resources/
PHP source code src/
test code tests/
Using this layout pretty much aligns your project with every other project you'd probably use as dependency. Note that you do not have to have EVERY directory, only the ones that actually host files.

The typical layout is this:
src/
vendor/
.git
composer.json
composer.lock
For compatibility purposes, the Git and Composer files should sit in the root of the project.
The source directory should be the root of the source files beginning with the top level namespace of your classes.
The vendor directory should contain all third party libraries imported via composer.
This is the minimum expected standards, particularly for a live project environment. If the project is a library, then the source directory could be omited.
The best example of this is the Symfony2 Standard Edition:
https://github.com/symfony/symfony-standard
After this the convention for frontend assets and controller is normally:
web/
js/
css/
images/
index.php
The index file in this case is your front controller.
This should include a bootstrapper in another directory, or initialize the environment using the configuration in that directory.
For Symfony2 this is the app directory:
https://github.com/symfony/symfony-standard/tree/master/app
Hope this helps, bare in mind this is based solely on my experience and observations.

Related

In a MVC system, where I put my classes that are not part of MVC?

For example: I have a session class, a message class, a CRUD class, in which folder I put them?
my folder structure is:
app/ <-- MVC logic
config/ <-- config files
core/ <-- core files of application, such as init
public/ <-- index and assets
vendor/ <-- for composer packages
I would recommend the Standard PHP package skeleton as a good root-level directory structure.
config/ # Configuration files
docs/ # Documentation and examples
public/ # Web server files (and front controller)
resources/ # Other resource files (assets, locales, migrations)
src/ # PHP source code (The App namespace)
tests/ # Test code
Extended version:
vendor/ # Reserved for composer (don't touch this folder)
tmp/ # Temporary files
tmp/cache/ # Cache files
tmp/logs/ # Log files
MVC directory structure (example):
templates/ # Templates (HTML, Twig files)
src/Controller/ # Controllers and actions, also src/Action
src/Domain/ # Business logic
I think your Session class is a HTTP Session library and the Message class is a validation class (or library). For this reason it should be installed via composer into the vendor/ folder.
It think your "CRUD" class is some kind of a "Data Table Pattern" or DAO or Repository and could be a combination of an external library vendor/ and application specific code src/.
What is "core" and "init"? Have you learned about MVC some abomination like CodeIgniter or something?
The "crud classes" (which actually should be data mappers) are part of model layer, same would apply to domain objects, repositories, services and other cod, that is strictly for implementing domain business logic.
The .. emm ... "session class" should probably do in the vendor, since there are dozens of implementations for session abstraction already available. And I have no idea what "message class" is supposed to be, but if you means "request" and "response", then those too should go in the vendor. I would recommend installing Symfony's HttpFoundation standalone component for those features.

Twig 1.x - what files can I safely remove for production?

Following an audit, I've been tasked to remove extraneous files from the Twig 1.x vendor directory in one of our sites. Planning on removing /twig/twig/doc, /twig/twig/test and see if anything breaks.
What about /twig/twig/ext/twig/run-tests.php, or the entire "ext" directory?
Does anyone have prior experience weeding a default Twig 1.x installation for production environments? Any assistance or advice gratefully welcomed.
The correct action would be to update your site's layout so that these files are outside your web server's document root -- then you don't have to worry about what to delete and what to leave. You probably have something like this, where your web server's document root is pointing directly at /path/to/project:
/path/to/project
/lib
foo.php
bar.php
/twig
/twig
/doc
/test
index.php
This means anybody can directly request http://yourdomain.com/twig/twig/test/some_file.php
What you want is more like this:
/path/to/project
/public
index.php
/lib
foo.php
bar.php
/twig
/twig
/doc
/test
Then configure your web server so that its document root is /path/to/project/public. Then your application code can still include() things in /twig and /lib, but your web server won't directly serve them.
If your removing files from the vendor directory they'll come back the next time you do a composer install so this seems kind of pointless.
The files you mention (docs and test) are causing no harm other than taking up space as they are not directly called, but that's just a downside to any package management system. Like #Alex said as long as they are not publicly accessible there is no need to worry.
If you really want Twig without the extra files you could fork the project, move it into your own Git repo then reference that in your composer.json instead of the official one - but you will miss out on any updates from Twig.
We're running on Apache, so the easiest solution was to simply add a .htaccess file to the top vendor directory:
# Prevent non-local access to the vendor directory.
Order deny,allow
Deny from all
Directory structure:
/root
/vendor
.htaccess
/twig
...
/includes
...
Now the PHP scripts continue to have access, but external attempts to view anything inside the vendor directory return a 403 error.

Where to put composer in a CMS that is going to be used in other projects?

Currently I am developing a small headless CMS. If anyone wants to use it he should just copy the root folder of my CMS and put it into his public_html folder. So for any requests to the CMS the URL looks like this:
example.com/my-cms-name/subfolder/...
To better organize third-party libraries I decided to use composer. But now I have the composer.json file and the vendor file in my CMS folder which is going to be in the public_html folder. So everything from composer will be available for everybody which is obviously not a good practice.
How can I overcome this problem? Should I do all composer things separately in a different folder? But then the user of my CMS has to include multiple folders into multiple directories which makes everything more complicated...
I agree to Half Crazed. Probably people will have to upload your CMS files via FTP anyway and set a root path. So you might as well divide your scripts in private and public ones. So a directory structure like this might be a good idea.
-config
-public_html (root path that people must point their domain to)
-css
-javascript
-images
-index.php
-.htaccess (optional)
-src (where your namespaced script should reside )
--MyApp
-vendor
-composer.json
-composer.lock
Update composer.json and add your own src, run update command. Then include the vendor autoload.php in your index.php and go the router/controller way.

Moving a Symfony project from a server with CPanel to Wamp

I'm having problems with Symfony. I recently got a Symfony project and I'm having problems moving a Symfony project from a server with Cpanel to a virtual server on Wamp. I haven't used Symfony before so I'm really confused on how the whole project would be opened in like a tree structure so that i can see whats going on.
Symfony's file structure is as such:
apps/
frontend/
backend/
batch/
cache/
config/
data/
sql/
doc/
lib/
model/
log/
plugins/
test/
unit/
functional/
web/
css/
images/
js/
uploads/
I presume you do not have access to the repository so you'll have to copy all the files to your WAMP installation. Generally you do not need to copy over the cache/* and log/* files. After copying the whole project to WAMP you can see if it's still working by running ./symfony.bat or ./symfony on linux/unix/osx.
Best way to copy the whole project is by creating an archive first, then extract it into your WAMP installation. In order to create tar.gz/zip/rar/tar.bz backups follow these steps:
Log in your cPanel and go to the File Manager;
In the left box, navigate to the directory / files you'd like to archive. Mark the items to be archived in the left box and choose "Compress" from the top menu

What is the most scalable PHP-based directory structure for a large site?

I am creating a very large PHP MVC-based site that will have a large library of php classes, javascripts, and many css files (not to mention a large amount of files for the MVC).
For the first time ever, I am actually taking the time to plan out a clean and organized directory structure.
What directory structures do you typically use, and which will be easiest to manuever when there are thousands of files?
This is my setup. It's worked great for me for small - very large projects (including a social network).
These folders would all live within my main application folder:
config - contains custom PHP config files
css - contains the project's CSS files
helpers - contains 'helper' files (each file is a collection of functions)
images - contains the project's images
js - contains the project's Javascript files
lib - contains PHP classes specific to the project
modules - My MVC framework allows packaging site sections as modules
blog - An example module
controllers - contains the controllers for the module
models - contains the models for the module
views - contains the views for the module
views - contains views that should be globally accessible (page header, footer, etc)
All the directories could obviously contain sub-folders that would further organize your files. For example, the 'css' folder could have sub-folders named 'web' and 'mobile'. The 'images' folder could contain a 'user_uploaded' folder which could then contain`'profile'. And of course you can add folders as you see fit, in one project I have a folder called 'uploaders' which just contains stand-alone upload scripts.
I also use convenience methods which help construct the filenames of what I want to load. For example, my loadView() will look for the view file in the current module directory, or if you pass an optional $module argument, it will look specifically within that module's folder.
I hope this helps.
You should have one directory as web root, where only files you want exposed to the whole internet should reside.
project/
web/
index.php
css/
js/
images/
config/
lib/
web/ is the root shown to visitors
lib/ is here the library folder, and where autoload look for files.
You can add more subfolders to project/ like controller, modules, view, helper, etc. This depends on your framework.
EDIT:
If you use composer (which I recommend) and maybe npm with grunt and less your file structure would be the following:
project/
web/
js/
css/
images/
index.php
cli/
config/
config.php
node_modules/
src/
test/
vendor/
composer.json
composer.lock
packages.json
web/ has all your public files
cli/ scripts and programs to be run from command line NOT the web
config/ has all your config files (in git you ignore config.php and instead have config.dist.php without usernames, passwords, validation codes and table prefixes/suffixes and other "secrets")
node_modules/ has all your library files from npm (in git I suggest you put this in a submodule)
src has all your local PHP files in psr4 structure, set up to autoload in composer.json
test/ has all your unit tests for your src classes, set up in autload-dev in composer.json (remember to use composer install --no-dev on live, maybe add -o if you don't have too many classes)
vendor has all your library files from composer and the ONE AND ONLY autoload.php to be included in web/index.php and any cli scripts (in git I suggest you ignore this vendor folder)
Add other folders and files as required for your project.
For deployment use this structure:
/sites/project/ (project is your projectname)
current (alias to current release folder releases/v1.1.0)
previous (optional alias to previous release folder releases/v1.0.1)
releases/
v1.0.0/ (git checkout of tag v1.0.0)
v1.0.1/ (git checkout of tag v1.0.1)
v1.1.0/ (git checkout of tag v1.1.0)
shared/ (has all your shared files and folders to be aliased in all releases - maybe something like GlusterFS)
Make a deployment script. Something like this:
First take backup of db or to copy it to a new database, checkout git repo to new folder with release tag, get all git submodules, run composer install --no-dev, setup any aliases for shared folders and files like uploaded images and configuration files, generate js/css with grunt and less or equivalent, point current alias to the new folder with the tag, run update database script, restart nginx/apache/fpm-php services, run tests to check the website is up.
Have a script to go back to previous version (or a guide so you know what to do).
For core files which are included:
approot/inc/
For data access functions and classes are in:
approot/dao/
For javascripts:
approot/scripts/
For CSS:
approot/styles/
For images:
approot/img/
For static content (normally for user profile pictures or uploaded images):
approot/static/
For caches:
approot/caches/
For templates or View files:
approot/templates/
All pages file:
approot/
Structure from Samstyle PHP Framework
The answer I posted here was from 2009. Over the years more standards were published, including PSR-0 which covers the topic on folder structure. I also have a new (and I feel that it's better) folder structure with Packfire Framework.
In my experience, you can never plan for this. You can try to follow what frameworks do, but I find I never quite fit exactly into their mold.
I recommend to just keep a good rule of thumb for 20 files in a directory maximum. If you find you need more, just create a few sub directories and move common components in there.
This is mostly a matter of preference, a quick Google search would reveal many different project structures. But it would be really nice if there were an agreed upon standard. I think this initiative by the PHP Package Development Standards is a good candidate.
This is the directory structure they propose:
bin/: command-line executables
config/: configuration files
docs/: documentation files
public/: web server files
resources/: other resource files
src/: PHP source code
tests/: test code
EDIT:
This is also mentioned in the PHP The Right Way under the section Common Directory structure.
I use codeigniter for small and big projects.
It's MVC feature is moderately good.
codeIgniter\system\application\config : contain all kind of configuration files like DB,Payment gateway, ftp config, routes and ...
codeIgniter\system\application\models: contain all kinds of database classes, you should create sub folders according to your need, I used customers, mailData, paymentModel, report, web-service and ....
codeIgniter\system\application\views: contain all kinds of files that will work as output for clients, you should think of reuse these files if possible. Like the models you had to create sub folder like administration, reports, email, email_template .....
codeIgniter\system\application\controllers : this is the most important part. This will help to create SEO url, so you should be more careful about sub folders this time. You can create like administration, products, reports, orders..... and consider a good name for the functions of the controller class.
These were for the PHP/HTML file.
Now about the other files:
codeIgniter\images: for the images
codeIgniter\scripts: for the Java scripts and their framework
codeIgniter\styles: for the CSS
codeIgniter\uploads: for the uploaded files, if you don't want to put files in the DB
For the detail see codeIgniter framework in detail.
Here "codeIgniter\" is the approot
This is the structure i'm using currently,
public/
assets/ /* js, css, imgs, ... */
index.php
src/
config/ /* for config files */
helpers/ /* for functions */
libraries/ /* for free classes that are not MVC classes */
models/ /* for M in MVC */
views/ /* for V in MVC */
controllers/ /* for C in MVC */
vendor/ /* for vendors files */
uploads/ /* for uploaded images, docs, ... */
Have a look at symfony 1.4 or symfony 2 dir structure. Choose what's most intuitive to you.
I believe this depends on how large the project will become. This is what I used mostly:
project/
index.php
img/
css/
js/
views/
functions/
As long as all the project files are organised...
Even though the question is abit old, I still think it is wise to suggest the latest scaleable application structure which I have been working in my SOA based application and working absolutely fine.
myApplication/
app/
config/
+ this can include custom MVC structure
cli/
docker/
lib/ - most commonly reusable components
logs/
public/ - should contain all publicly exposable web contains
sql/ - db migration stuffs
tests/ - compulsory test
tools/ - application addon tools like any kinds of rulset etc
vendor/

Categories