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/
Related
I have an existing website running on PHP, MySQL CSS, JS and Ajax.
For future projects, I would like to use the Laravel framework to create new pages or functionality to the existing website... let's say a blog, for instance.
Is this possible?
Is the Laravel framework compatible with an existing website that is not set up using the MVC framework?
The reason is that I would like to make my work easier and more efficient and the use of a framework would be helpful.
Please let me know if you need any other info, I know the question is a little broad.
Thanks,
Justin
yes it's possible but of course maybe with difficulties.
you should put your web files in /views directory, convert your php files to controllers or library files and put your assets in /public directory.
then you can assign routes to your controller and call it form outside of the application.
main job is put your code into controller or libraries. is it possible to split your code into separate logical units? and extract model logic (as php mvc says) from your main code.
so if you have a clean code with good design and minimum dependencies you can think more about it. this could be a good challenge if you go through it cautiously. be careful to not fail and waste your time.
I am making a controller for each type of request post/put/get.
So my question now is, what is the best way to put controllers in subfolders when using L 4.2 ?
/controllers/subfolders..
I've seen some people using namespacing and some people simply makes a subfolder and put their controllers in it then run composer dump autoload-
But is there any "best practice" way to do it in L 4.2?
I do it the namespace way. One advantage this gives is that we can have same named classes inside the folders. Currently in a Laravel 4.2 app that I'm building, I am using controllers/api subfolder, with the namespace of Api for all the classes in it. And one of the classes is UsersController. Which might also be used for the frontend website, so now you will have to say FrontendUsersController, or something weird and long. So to avoid this, better get in shape with namespaced controllers.
Also, Laravel 5 advocates namespacing for your project, so does PSR standard. So this is probably much better way in the long run.
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.
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.
What is the most efficient way of organizing PHP code for a CRUD application? What are the well-known naming conventions?
I have a class and its getters and setters just seem to be SQL queries. Is this correct, or how am I supposed to organize it?
You should have a look at DomainModels and DataMappers. In general, I recommend the Zend Framework and its Quickstart Tutorial, as it teaches these two very relevant patterns in a few minutes by example. You do not need to switch to Zend Framework afterwards, but it is good learning material.
Use the MVC pattern. Then you allready have files in the folders:
models
views
controllers
inside this folders I would recommend not to use prefixes or sufixes because the files are already in such an folder. But extend all controller files and model files from one main class!
Maybe you want to download and study a example project made in CakePHP framework which is very well structured and uses CRUD as well!