What do you name your files when using MVC? - php

When using the MVC pattern, which I'm not terribly experienced with, I find myself naming things like this:
/app/views/widget.php
/app/models/widget.php
/app/controllers/widget.php
That appeals to me because it's easy to find associated classes, and I lean towards shorter names when practical. However, when I'm looking in my IDE, I see three different files called widget.php, which is confusing. I'm tempted to add "_v", "_c", "_m" or something to each name. How do you handle this?
FWIW, I'm using CodeIgniter at the moment, and I don't know if there are any special benefits to using a particular convention, or any standard practices. Regardless, I'm intersted in the best-practices from various platforms.

My view ends in phtml, so that would make Widget.phtml. My model is a Widget so that would yield Widget.php, just like that, and my controller would be WidgetController.php.

I personally think that having everything named widget.php gets confusing even if the files are in separate files. I tend to append either Model, View, or Controller to the end of the files names in addition to having the files divided into respective folders. While it is more verbose it is much more expressive and easier for newcomers to your code base to follow your code. So my widget (in Java which is what I most frequently use for mvc) would be named as follows:
/app/widget/view/WidgetView.jsp
/app/widget/model/WidgetModel.java
/app/widget/controller/WidgetController.java
/app/coolwidget/view/CoolWidget.java
...
Plus when I'm in an IDE or editor I tend to look at the file name and not the full path to the file when editing. So if I'm editing the Model, View, and Controller for my widget, I don't want to be examining the file paths to figure out which one I'm working on.

CodeIgniter (and for that matter any framework) relies on a set of 'conventions' (rules basically). One of those rules is how routing is handled. For example, the 'widget' file you have in /app/controllers/ will translate to a URL of http://yoursite.com/widget/action/ (where action are function names in your Widget class.
The normal convention is to use CamelCaseNaming for your classes and lowerCamelCase naming for methods. Each framework has a different routing engine. If you have class WidgetBlahBlah will translate to a url of /widget-blah-blah/ or /widget.blah.blah/ (depending). Action names and routing are similar.
As for naming of views, views should be named the same as your actions. They should be organized into sub directories based on your class names. Again, this is all convention. Actions in your classes look for views in specific locations named a specific name.
If you're going to use MVC I would suggest going back to the beginning and learning how to use it. MVC is designed to be rapid development by understanding a set of conventions and leveraging them. Maybe start here: http://codeigniter.com/user_guide/toc.html

I generally just leave them named without any special convention, and distinguish between the different files by looking at:
- folder names
- file contents
With syntax highlighting, a view file containing mostly html is very easy to distinguish from a controller or model. As for telling the difference between models and controllers: I don't generally name models and controllers with the same names, so there isn't a problem there for me.

Related

Yii2: a proper way to resolve ambiguity of controller and module names

As far as I know, currently Yii 2 doesn't have an out of the box method to resolve ambiguity of controller and module names. An example of module hierarchy to describe what exactly I mean:
app\modules\v1\controllers\UserController // resolves the /v1/users and /v1/users/{id} actions
app\modules\v1\modules\user\Module.php // nested module, resolves the /v1/user/... controllers and their actions, e.g. /v1/user/something/{id}
In this case, the UserController conflicts with the user Module. The main reason of the ambiguity is the singular-plural magic of Yii 2 framework. I didn't find an appropriate solution to resolve this ambiguity. Further my ideas how to resolve it.
Rename the module.
Rename the UserController to the UsersController.
Create an additional submodule, and place there the UserController. E.g. app\modules\v1\modules\root\controllers\UserController
I'm not sure that at least one of these options is a quite elegant one and a proper solution in view of the Yii 2 philosophy.
Coming back to the main question, what is a more appropriate approach to resolve this issue by the Yii 2 philosophy? Controller and Module is two different types of objects, which is differently pluralized or not, thus should be right way to separate them in the routing for the described case.
How I usually deal with this depends a bit on how I'm structuring things.
Let's say I have users, departments, orders and maybe some other stuff. All these concepts have their own module in which all interactions take place. If I want to create REST controllers for all these concepts I can choose to add a controller to every module or I can create a dedicated API module for the controllers.
When creating a dedicated module for this purpose I usually name it api, maybe with some nested versioning module(s) inside. So in this situation I would get the following structure app\modules\api\controllers\UserController which would result in the URL /api/user. No ambiguity there and pretty clear what this is meant for.
When adding such a controller to the module itself I choose a better name than just 'UserController'. When asking myself the question 'What does this controller accomplish?/What does it do?' I find that just UserController doesn't cut it; Especially when inside a User module, resulting in /user/user. This controller is probably going to exist alongside one or more different controllers in the User module, all meant for something different. So, usually, I end up naming it just ApiController, resulting in /user/api. Another controller could be the ProfileController. So when looking at the URLs it's pretty clear what /user/api and /user/profile do. Without the ambiguity.
I am not sure I can fully understand the question, but probably you're asking about classname aliases?
use My\Full\Classname as Another;

Efficient way to organize View files in Laravel 5.3 when coding with PhpStorm

I don't know if this question qualifies to be in SO, but since PhpStorm became de facto the IDE to develop in Laravel framework, this could be helpful to many of us. Since file naming in Laravel is a big deal, I was wandering what would be a proper/efficient/logical way to name View files.
Lets say I declare all my routes in web.php. I have multiple tables, all named as word in plural like articles. So I have Article model and ArticleController resource controller. Controller consist of standard RESTful and nonstandard routes.
The way I wanted to name my View files is pretty much straightforward and logical, eg:
for home in web.php I have /resources/views/home.blade.php and define it with view('home')
for index in ArticleController I have /resources/views/articles/index.blade.php and define it with view('articles.index')
for show in ArticleController I have /resources/views/articles/show.blade.php and define it with view('articles.show')etc...
Problem arise when I want to use famous PhpStorm CTRL+SHIFT+N open by file search, I get filenames with the same filename, like show.blade.php. As far as I know, there is no way to search by a path name...
So, I decided to rename my filenames to articles_show.blade.php, articles_index.blade.php, but pretty soon I my views folder was overcrowded. I have considered articles/articles_show.blade.php pattern, but it's not following DRY principle and look ugly when defined.
Do any of you managed to develop a solution for efficient and logical naming convention? How do YOU organize your view files?
P.S. I still use original pattern for vendor views, like auth/login.blade.php, auth/register.blade.php. Layouts files also follows that pattern: layouts/app.blade.php (popular layout name), layouts/header.blade.php, layouts/footer.blade.php. But it's obvious, these filenames will not be repeated, so no problem with search function. I want to also mention, that for most tables/controllers/models I have a special loop view file, that is not bound to any function or route, and named by established pattern, like articles_loop.blade.php.
It looks like you can search for a filename by a path! Kudos to #LazyOne, since he pushed me in the right direction.
So, in order to search for a filename within a path using a partial match, all you need to do is suffix a partial path name with "/" and type in a partial of your filename.
So in order to find /resources/views/articles/show.blade.php, you can put either:
ar/sh
vi/ow
ar/shbl (notice how I used two partial sh and bl to find show.blade.php without any separator whatsoever).
etc..

Handling Sub-Sections of a Site—Codeigniter

I'm fairly new to Codeigniter as well as MVC and I'm having a bit of trouble figuring out the best way to accomplish this.
I need to build an app that allows users to apply to various programs offered by some institutions. However, these institutions must all have a spot in the app yet they want their independence from one another—not sharing one application page for all programs. For instance Institution 1 wants a section of the site to only view and apply to their programs and Institution 2 wants a section of the site to only apply to their programs.
What is the best way to accomplish this? Should I create a separate controller for each institution?
E.g. sitename.com/inst1/apply, sitename.com/inst2/apply
Each of these controllers would essentially be identical with the same create/read/update/etc functions though. What are best practices in this situation? Thank you!
You can create folders to serve your functionality properly. This is widely used for APIs.
For example. You can have your folder structure like this.
- application/
- controllers/
- inst1/
apply.php
- inst2/
apply.php
With this, you'll have the URL endpoints like.
index.php/inst1/apply
index.php/inst2/apply
I think you have it right, you'd create controllers for each institution allowing you to change what data you were pulling for each. The views could be shared since all the functionality would be in the controller/model which is one of the more important aspects of MVC to begin with, the ability to separate those layers and reuse what you need where you need without duplication. If you set up your pages as a template you could even pull different templates to feed the views to that would be institution specific.
For this you probably want to use the same controller and instead handle the variation through passing your function a different uri segment which you can read about here
. In my codeigniter applications i like to keep a specific functionality within each controller or model. So it might look something like:
sitename.com/my_controller/my_function/my_argument
Where the function in your controller looks like:
public function my_function($argument){
//stuff goes here
}
You can of course use your routes file to make the url look however you'd like.
Just build a single controller, and make a flag to differ them. In the view file you may check for this flag to decide weather to show programs and apply or not.
Your url would be like that:
sitename.com/inst/1/apply, sitename.com/inst/2/apply
note: you may also change the numbers in the url with words; to better seo.

Is my file/class naming convention OK for codeigniter?

I am starting a new project and want things to be done right:
Controller:
file name: routemanagerdashboard.php
class name: RouteManagerDashboard
Views:
file name: routemanagerdashboard_dashboard.php
Model:
file name: routemanagerdashboard_model
These naming conventions are perfectly fine. Although there are some conventions you must follow, what you name your files/classes mostly comes down to personal preference. After working with CodeIgniter for a couple years I've settled on the following naming conventions for my files.
Controllers: companies.php (plural file and class name)
Models: company_model.php (singular of the table it corresponds to)
Views: companies/method_name.php (all views for the controller go into a folder for views in that class)
I always make my database table names plural and have a one-to-one relationship between my models and tables. Controllers usually have a corresponding table and model, but not always. I also use a base model which removes a lot of the tedium of setting up a new model for every table which fits into this setup very well and is probably a big reason that I stick pretty strictly to it.
This is fine, but routemanagerdashboard seems a bit verbose, you may consider coming up with something shorter. If you like the controller class name but hate the long urls, you can always use a route to get around it.
Since we're dealing with PHP, case sensitivity is not an issue, so your controller class name is fine, just as long as it matches the characters in it's file name. Just stick to lowercase file names.
Your model name is using the common CI convention, but be aware that when loading the model you may use the second parameter to alias the class name for easier use. Example:
$this->load->model('routemanagerdashboard_model', 'r_model');
$this->r_model->get_something(); // A little easier to work with
On views: It's probably better to create a subdirectory for all views that are directly related to the controller. You are probably going to need more than just one view file per controller. You will see your /views folder become unmaintainable if you put all view files directly in the root of it. I suggest you use sub directories and short meaningful file names, perhaps matching the name of the controller method you intend to load them in. Example:
// file = /views/routemanager/dashboard.php
$this->load->view('routemanager/dashboard');
So, yes - you're Doing it Right for the most part, but coming up with shorter names if possible, and organizing your view files into subdirectories will probably be a good idea.

Django development - how to lay out and use code properly

Hi i'm a PHP developer trying to convert to Django and im having a bit of a hard time understanding where to put things and how to use the new language. Basically what i want to know is for example in codeigniter i would make classes and functions in my controllers. Where do i do that in django?
I believe the 'view' in django is more like the controller in an MVC framework but all the examples i can find of the view are very simple and just call a template and pass it some data.
I currently have an index view and a 'rates' view in my current project. The index page will call rates via JS and pass it some GET variables. In my php version i used these to instantiate my rates class which when had all the needed functions in it. I want to do this in Django.
The reason that it is called, "MVT" instead of "MVC" is that "View" to Django means, "presentation of data (according to given logic)" and "Template" means, "display of data presented." In a traditional MVC paradigm, "Controller" means "executer of logic" and "View" is "presentation of the result of the executed logic". (They are almost the same idea, but not quite).
So, what does this mean? Basically, if I were building something in Symfony, I would put all of the logic in the sfAction components. In CodeIgniter, it would be in the CI_Controller. In Django, I will place all of the logic in the "Views".
Just like CI (and Zend and others) will then call a "View" from the Controller descendant, the Django view will also call a "Template" from its "View". (Symfony's views are often called in a different syntax, so I will leave that to the reader to research if so desired).
Looking at your example, it looks like you want to call a method in the "View" (which view is configured in urls.py) which simply instantiates another object which has "all of your logic in it". Something like:
def ratesHandler(request):
rate = MyRatesClass(request.GET)
return HttpResponse("Insert something here. ") #or render_to_response
The logic always belongs in the view. You can put whatever logic you like there - no need to put it into a class, unless you want to. If the logic is related to a specific model, though, it is best to make it a method of the model or the model's Manager.
In django: "models" go in models.py, "controllers" go in views.py and "views" go in templates.
Models tend to be classes that subclass django.db.models.Model
Controllers (in views.py) are often functions but you can use classes if you like.
Note that if you're just displaying data from the database you can often use generic views so you hardly have to do any coding at all.
I can't tell whether there is a question in the last 2 paragraphs of your post. If there is a question there, please edit your post so it's clear.

Categories