Good evening!
I have a little problem accessing custom classes in symfony2.1.6 using the
autoloader.
What I need to do is access the
Mangress/src/Qkprod/Classes/api/APIEntry.php class from within the
DefaultController. Apparently I am too stupid to find a way to extend
the autoload.php to do that. Now I am at a point where I have been
reading so many blogposts and solutions that I don't know anymore
what to do anymore...
My file structure looks as follows:
Mangress
-app
-src
-Qkprod
-MangressBundle
-Controller, Entity, Resources, Tests
-Classes
-api
-APIEntry.php
-db
-security
-QkprodMangressBundle.php
-vendor
-web
The Classes folder contains all the classes I have written so far.
What I have read so far was that I need to register the Namespaces of
the classes to load in the Mangress/app/autoload.php
Autoloading a class in Symfony 2.1
$loader->add('Qkprod\MangressBundle\api',
DIR.'/../src/Qkprod/MangressBundle/Classes/api/');
Though this seems to be wrong..
Is there something wrong with my naming or understanding of symfony?
I register the Namespace "Qkprod\MangressBundle\api" to the autoloader
so that symfony knows where to look when
use Qkprod\MangressBundle\api\APIEntry;
So it has to look in my Mangress/src/Qkprod/MangressBundle/Classes/api
folder am I wrong?
I would really appreciate some guidance :)
Why aren't you using the directory structure recommended by symfony developers?
And not even just symfony developers, this structure is called PSR-0, and is adopted by many PHP frameworks.
If you stay with the conventions, the default autoloader will load your classes just fine, and other developers will understand your code more easier.
Just remove your "Classes" directory, and move everything from it a level upwards:
Mangress
-app
-src
-Qkprod
-MangressBundle
-Controller, Entity, Resources, Tests
-Api
-APIEntry.php
-Db
-security
-QkprodMangressBundle.php
-vendor
-web
This way when you write
use Qkprod\MangressBundle\Api\APIEntry
It will know where to look.
Related
I'm working on a PHP project with Laravel 5 and I'm thinking of setting up a different folder structure for it.
The standard Laravel folder structure is something like this:
/app
/commands
/Http
/Controllers
/Middleware
Kernel.php
routes.php
/Providers
Model.php
/config
/resources
etc...
However, when the project grows larger and you have a lot of Controllers/Repositories/Models and such. This structure is gonna break.
For example: it's not very easy to find a bug in your admin panel if you have to dig through your routes, find which controller is responsible, find that controller amongst a large set of controllers, find out what that does, find out other possibly responsible classes in other large folders, and so on. In short: it's a mess.
I've been looking at ways to break the structure into modules. I've come up with a way to do it, but I'm not sure if it's a good way.
I would make a folder each functionality and put all the related code together. For example:
/app
/Admin
/Controllers
/Requests
/Models
routes.php
/Products
/Controllers
/Requests
/Models
routes.php
etc. (you get the point)
Instead of initializing 1 router from the standard RouteServiceProvider.php, I would have to write a ServiceProvider for each module and start all individual routes from there. So in this case I would have an AdminServiceProvider and a ProductServiceProvider which each require the routes.php file in their own subdirectory (and with that their own controller namespace).
This seems to solve my case for now, but I'm wondering if I'm gonna run into trouble with this setup. All the examples I can find on the web just stick to the standard structure. Can anyone tell me if this is a decent way to do it? Or does anyone have an alternate way of doing this?
This is a good way you proposed but there is no need to do it on your own. At the moment I'm working on a L5 project that uses modules - each of them have directories for repositories, models, own route file etc. I'm using Caffeinated module for that
I also believe the structure looks good. I would also add on an additional, core or base folder which will contain base classes and reusable componets, to ensure you do not have duplications of code in the separate "modules"
You can also checkout this interesting presentation at a Laracon, September 2014: Laracon 2014: Dayle Rees - Breaking
The Mold
Also have a look at the source for October CMS and Doptor
CMS(claims to be moduler), you may glean a thing or two. (Have
used neither, by the way).
Otherwise, do what I did and Google Opensource Laravel projects.
You get good insight from them. Especially those that a featured or
are organised, with growing communities.
I'm trying to learn zend framework. I managed to install it on my localhost. However i'm having trouble understanding the folder structure? There are 5 main folders after installing the skeleton application - config, data, module, public and vendor.
I've seen some proposed folder structures online, but how to I go about it? Do I just create folders like views, controllers, models etc?
Thanks!
Vendor is where composer installs dependencies and libraries, config is where configuration lives, data is for cache etc, public is where your index.php and css/js/img assets are, you are really interested in module directory that contains application modules. For the start you only really need one module - Application, inside this directory you should have config dir that has module specific config, Module.php - module bootstrap file, view with templates structured per controller and src folder with your code. Inside your src file there is your Application module namespace directory that is placed in Application directory to mimic PSR-4 autoloader namespaces it can contain your application code in this example directory structure: Controller, Form, Model. Model can contain Service, Repository and Entity folders
If you just got started with ZF2 I suggest reading some documentation. Basic things like this can all be found in the documentation. For example here you find more about the folder structure.
I would also suggest taking a look at the ZF2 Skeleton application documentation/tutorial since this will help you understand the basics of a ZF2 application. Here an example on how to structure a new module. Building the album application yourself is a really nice way to get started.
At this moment, my framework's directory structure looks like this:
framework/
libraries/
autoload/
autoload.class.php
resource.namespaces.php
router/
tests/
router.test.php
router.class.php
resource.routes.php
configuration/
framework.configuration.php
router.configuration.php
controllers/
index.controller.php
models/
index.model.php
views/
default/
index/
index.view.php
header.view.php
footer.view.php
assets/
css/
javascript/
images/
index.php
When my framework was smaller, it was a lot cleaner. I have give a look at other popular frameworks. They have two main folders:
framework/
app/
web/
Actually, this structure is very clean and nice because we separate the front-end and the back-end. But I wonder what should I put in each of these.
Logically, libraries folder should be inside the app folder, but it is not really a part of the application. For me, an application has models, controller and views. I think libraries should be put outside. Where should I put my libraries?
I has a lot of PHPUnit tests in my libraries. Should I add a folder named tests inside each library, or should I put it inside/outside the app folder?
If I want to implement a template feature, where I could choose which template I want to use for my website, how could I organise it? Every template has differents elements, so header.view.php will not be the same, etc. Now, I'm creating a folder in views which is the template folder. But I think it is a bad idea, 'cause now, for every template I must recreate all the views.
In a lot of applications, there is a vendor folder that contains all the main classes. Is this the same as my libraries folder? Does it have the same role?
I also have some 'resources' files (eg.: resources.routes.php). They are used to add some routes/namespaces. It's a bit like a configuration file. Should I create another direcotry for these files, or put them inside the class which they refers to?
I'll try to cover all your questions.
The vendors folder that you see in many apps is from composer. In that folder are all the dependencies of your application/framework (often libraries from other people).
Composer allows you to pull in some good components for your framework so that you don't have to write everything from scratch. Say for example I want to use FastRoute because it is much better than any router I am capable of writing myself. So I just add the following to my composer.json, run composer update and I can use it in my framework.
{
"require": {
"nikic/fast-route": "dev-master"
}
}
You can also use composer to autoload your classes so that you don't have to write your own autoloader (and cache that for production).
I really dislike your current file names. adding .class to a filename makes absolutely no sense to me. The dots also just complicate Autoloading. I recommend you have a look at PSR-0 for some inspiration on how to handle file names/namespaces (This is supported by the composer autoloader).
For the tests you could create a Tests folder in the root folder of your project and in there recreate the directory structure of your project and add the test classes in the matching folders. You can also put your mock objects in there. This makes it easy to just run tests for a certain part of your application.
I also recommend that you move your application out of your public folder and instead have a public folder with only an index.php and your assets. That index.php does nothing other than requiring your front controller that bootstraps your app. Doing it that way adds another layer of security if for some reason your webserver stops processing PHP. Otherwise a visitor would be able to see your whole source code.
To get some inspiration for your folder structure questions, I recommend that you have a look at how other people solved this issue. Arya and PitchBlade would be a good starting point because they are way smaller than a framework like symfony.
I hope this helps. In the future I recommend that you split up your questions into different SO questions, that makes answering easier and you will get better answers because people who know the answer to one of your questions don't also have to answer all of them.
I have been suffering from this problem for several months now. There are lot of tutorials explain about how to implement your code using OOP methods. With parent classes, abstract classes, Interfaces etc.
My problem is where should I create such file structure in frameworks as Codeigniter, Laravel etc. I know only controller folder to define classes. Should I place all the interfaces and parent classes in controller folder? Please explain it's really mess for me.
This tutorial helped me understand where to put my own libraries. Give it a shot!
In Laravel 4, the file structure already exists in your /app folder.
See: http://laravelbook.com/laravel-architecture/
This is my first question here. :)
I am working on a little php framework and started to think of ways to re-use the same code between multiple projects. Right now with this framework it is possible to make multiple application directories for different projects and use the same core - similar of what codeigniter 2 does.
The question about code reuse raised at work when I needed to make a website that is quite different from existing one, but would still use classes from it.
My first two ideas was either use some kind of a global "models" directory where to place files shared between multiple projects (and add option to framework to load them), or to add a possibility to load these "models" from other project(s).
I thought maybe somebody else have some better ideas and wanted to know other developer thoughts on the subject in general.
As an example this could be the current directory structure:
live/ - live site
config/
controllers/
helpers/
models/
public/
views/
admin/ - administration (same structure as "live/")
system/ - framework core
Well I don't think loading models or other classes directly from another project is a good idea. If two or more projects share the same classes, they should be located somewhere outside of both projects. This is the situation where the codeigniter packages comes in handy. It allows to have separate folder for all of your libraries, models etc. and load them in any codeigniter project very easily. Take a look at the official documentation for more details.