Having gone through numerous laravel tutorials and examples, i have seen source code with controllers, models and views in very different directories.
The question is, where should they go, and how does the system (which presumably requires convention) work if they are put in a different place?
E.g. the official laravel quick-start project puts them in:
app/Http/Controllers/xxxControler.php
app/Http/routes.php
resources/views/xxx.blade.php
Cant find where it has put the models.
However, the simple laravel crud master tutorial puts them here:
app/controllers/xxxControler.php
app/models/xxx.php
app/routes.php
app/views/xxx/create.blade.php
app/views/xxx/edit.blade.php
Controllers
If you look in the 5.4 documentation here it says controllers should go in app/Http/Controllers.
If this is the case, how did the latter example work (as the controller files are in the "wrong" dir, and I assume that laravel, like grails, relies on convention over configuration)?
The latter example directories seem much more logical than the official place, as a controller has nothing to do with Http - its part of the app logic.
Models
The official documentation mentions that "models typically live in the app directory". This implies they can be put put in app/models, which is good.
views
Unfortunately, the official documentation says they have to go in resources/views. Again, this would be far more logical to have views, controllers and models all being together as sub directories of app, as per the second example above. Is it possible, and advisable, to use this logical, but unofficial structure? as a beginner, I have difficulty in finding the models, views and controllers as they are put in 3 different directory paths with no obvious logic.
routes
The official documentation says that routes should go in routes/web.php. I have not found an example project using this convention - I have seen them in app/routes.php, and app/http/routes.php. Assuming its ok to put them in random places under app, how does one configure where the routes definition files go?
For Laravel version ^5.4
Controllers should be in this directory
app/Http/Controllers/xxxController.php
Models should be in this directory
app/xxx.php
Views should be in this directory
resources/views/xxx.blade.php
Routes should be in this directory
routes/web.php // for web routes
routes/api.php // for api routes
These are all recommended places to put controllers, models, views and routes. You are free to modify everything of course!
Older versions of Laravel have different places for Controllers, Models, Views and Routes. Check the documentation where they should be.
The thing is that you must recognize the Laravel version because Laravel went through many changes between years 2011 and 2016...
That link in your question for example is from year 2013 which is very old.
And I strongly recommend you to follow the Laracasts tutorials at laracasts.com
Related
I've started with Codeigniter 4 today. I have an app that I've built with Codeigniter 3 and now I want to upgrade it to CI4. The first thing is to upgrade routes. I have a simple but GREAT solution by enabling users to define their own routes with a simple page, like this:
And it was super easy, just needed to load and update Routes.php file - arrays of routes.
Now, as I am looking at app/config/Routes.php in CI4 I see it is not a simple array but there is much more to it. So, my question is what would be the simplest way to enable users to define their own routes with a webpage?
is it important to use the same directories for views controllers and models or you can be free to create files inside created folders and refer to them when needed
in Laravel MVC
Check docs Directory Structure
The default Laravel application structure is intended to provide a great starting point for both large and small applications. But you are free to organize your application however you like. Laravel imposes almost no restrictions on where any given class is located - as long as Composer can autoload the class.
I myself haven't tried doing my own folder structure. I would personally stick with the standard created by Laravel and follow that.
I have been working on laravel and have been doing some routing. I was just wondering on what is the difference between writing the route as:
route::get('roles/{id}/edit',rolesController#edit);
versus
route::get('roles/edit/{id}',rolesController#edit);
One difference is clearly visible and that is the placement of the id variable. Can't figure out any other reason. Please provide an explanation on this.
Other than the actual look of the URL, there's no real difference as far as the framework is concerned.
I suppose it's the matter of preference when using any of this. Maybe, for example, if you are giving options of editing the user profile and posts, this might come handy as both are different routes, technically
No difference. It depends on you how you would like to build your routes. But try to user best practices which laravel creator recommend (https://laravel.com/docs/5.7/controllers#resource-controllers).
And also i want take your attention on how you called your controller. You should use CamelCase for naming your files (https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md).
There's no difference, but you might want to look in reosource routes and controller. Basically, laravel framework automatically creates routes and methods for controllers that you might need in your project. For example:
If you create a contoller like this:
php artisan make:controller RolesController --resource
and create a resource route like this:
Route::resource('/roles', 'RolesController ');
framework automatically crates this routes for you:
Verb Path Action Route Name
GET /roles index roles.index
GET /roles/create create roles.create
POST /roles store roles.store
GET /roles/{roles} show roles.show
GET /roles/{roles}/edit edit roles.edit
PUT|PATCH /roles/{roles} update roles.update
DELETE /roles/{roles} destroy roles.destroy
So you don't have to make your own routes and ask yourself if they are correct or not. Look into laravel official documentation for more info about this.
I want to organize Laravel code into below folder structure for the sake of easy maintenance and better grouping. I do not know whether I can do this in Laravel.
app
--order
----CustomerOrder.php
----CustomerOrderController.php
----CustomerOrderService.php
--purchasing
----PurchaseOrder.php
----PurchaseOrderController.php
----PurchaseOrderService.php
Can I modify configuration files to facilitate above hierarchy?
Laravel is a MVC-based framework. The controllers and models are placed in different folders as the documents instruct:
https://laravel.com/docs/master/structure
I suggest you organize and group your project folder structure according to Laravel document. The readability and maintenance will be much easier if everyone using the same folder structure in same framework rather than the customized structure.
i need to load a library from my module using Modular Extensions
my structure like this
modules/
modules/categories/library
categories_class.php
modules/categories/controllers/
categories.php
I need to load categories library in categories controller .
any one cane help me?
I see two problems..
Problem 1
According to your question, your categories module is not organized properly. The whole purpose of HMVC is compartmentalizing of code e.x; modules. Given your present question how does that structure allow you to copy your modules folder and paste it into another app? Answer: It doesnt..
Follow the example below
It should be the following from the app root:
/application/
config
modules/
categories/
views
controllers/
categories.php
libraries/
categories_class.php
models
libraries
core
controllers
Problem 2
per the user guide: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
You must prefix the module name in front of anything referenced inside the modules folder.
e.g: $this->load->library('module/library');
or in your case: $this->load->library('categories/categories_class');
I have attempted previously to exclude the modules folder name and have never gotten it to work.
Controllers can be loaded as class variables of other controllers
using $this->load->module(’module/controller’); or simply
$this->load->module(’module’); if the controller name matches the
module name.
Any loaded module controller can then be used like a library, ie:
$this->controller->method(), but it has access to its own models and
libraries independently from the caller.
I have another perspective for this error behavior, that make me spend about 3 hours, actually I am always using combination of Uppercase and Lowercase on my custom libraries on Codeigniter.
For wiredesigz of Codeigniter HMVC, remember the libraries loader behavior as same as CI itself, always use lowercase of your library classes name instead of actual class name (Maybe combination of uppercase and lowercase)