Is this the right way to structure MVC (understand

2019-06-09 08:22发布

问题:

DO I UNDERSTAND MVC CORRECTLY?

I've read many posts by the user teresko and I think I finally understand the real concept of modern MVC. I made a diagram of how I understand it to be, I'm hoping somebody can confirm this for me and that maybe this diagram will help some other users.

IS MY PLANNED DIRECTORY STRUCTURE "SMART"?

Also I'd like to get into folder structure. I'm not too sure that I plan on using a very smart/efficient way of structuring them. In my root directory I have 4 folders. The model folder contains 3 subdirectories called data_mappers, domain_objects and services. The presentation folder contains 2 subdirectories naturally called views and controllers. The 3rd folder in my root directory is one of the ones I'm less sure on called lib and contains another 3 sub-folders, scripts, themes, and templates (is this a good place for templates that will be called by view?). And the last folder which I have is called config (if anybody can think of a better name suggestions are welcomed). This is where I will place the router and base classes and any files of that type. I'm not just going for functionality with my framework, I want it to be technically sound, any suggestions/advise are welcome, I want to make sure my understanding up to this point is correct before I start making the index and router etc so I don't have to start over once again. Thanks

IS THIS A GOOD WAY TO GO ABOUT MULTI-LANGUAGE SUPPORT IN MVC?

(A little off topic, but I need to incorporate this into planning my MVC framework, it may be helpful to other users, and in order to answer the question clearly and concisely the structure of the website needs to be understood and it happens to be described in detail in the above)

My website must be available in English and French, both of which I happen to speak. I plan to create two more sub-folders within the templates folder, one called en and and another fr and simply place all of the English templates in en folder and translate them into French and place them in fr folder and set up the website so www.mywebsite.com/en/home shows the template in English and www.mywebsite.com/fr/home shows the template in French.

There will be a main language selection page that will redirect you to either en or fr. Then, I plan to store the 2 letter language code in a $_SESSION variable. The view will use this $_SESSION variable to pick which template to display. This is the way I had done it on the old version of my website. Is this a good way of accomplishing multi-language support in MVC or is there some better way?

What if I want to make the url also appear in French? If I put the view files inside more subfolders and instead of naming them like this: "/presentation/views/news/news.php" name them like this: "/pre sentation/views/news_nouvelles/news.php" and then make the router (urls and routing is one of my weaknesses, but I will learn on my own ;) store everything before the "_" in a variable corresponding to the English url, and everything after corresponding to the French url. This is just a theory and I have not attempted what is in this last paragraph yet, the multi-language support is more for discussion than a question, although once again, it is relevant because a thorough understanding of my framework is needed to answer/discuss language support.

回答1:

This really isn't all that related to MVC. When you are implementing multilingual URLs, it's all about routing.

Routing

Basically, by the time your code gets to calling stuff on controller, you should have fully initialized request instance, that has recognized language from the URL and translated all the parts of input into something useful for creating instances.

$request = new Request($query);

$router = new Router;
$router->import('/path/to/routes.json');
$router->route( $request );

At this point you would have split the user's query (from example: '/presentation/view/dernières/nouvelles') into recognized segments, based on pattern that was recognized.

Lets take for example, that matted pattern was in config defined as:

/presentation[[/:action[/:filter]]/:resource]

And the produced result is:

resource   >> 'nouvelles'
action     >> 'view'
filter     >> 'dernières'

Internationalization

The translations will usually be stored either in database or in some configuration file. If your service factory is already initialized at this point, then you can dip into that for facilitating the interaction with this stored config:

$normalizer = new Normalizer( $serviceFactory->create('translation') );
$normalizer->adjust($request);

What you would collect from stored config by translating it would be

  • translation of the value that user provided in query
  • language in which that query fragment was written
  • title which corresponds to the fragment

The data that you get back would look something like:

 translation  |  language  |  label
-------------------------------------
 view         |  en        |  view
 dernières    |  fr        |  latest
 nouvelles    |  fr        |  news

Note: if you return also the queried value, then you can perform such operations with a single SQL query using
.. WHERE translation IN ( <the list> ), which let you collect all in a single request.

These entries are used for two tasks:

  • actually translating the request so that the matched parameter will actually have classes/methods that correspond to them
  • for determining, which language user might expect

By the end of this you should have a translated ll the parameters in the Request instance and, based on some criteria, determined and added language parameter to that request.

Note: browser also sends some language-related information to you in the headers. Depending on your setup, you could user Accepted-Language header for figuring out which language user might prefer.

my 2 cents