0

Okay, I'm learning all about MVC, Bootstrap, Ajax and Smarty and I understand the basic principles of MVC. I am having one major issue however and I just cannot wrap my head around it, no matter how much I try and no matter how much reading I do.

At it's very core the thing I cannot seem to get my head around is how to pass variables from view to controller from controller to view.

If I want to assign a variable, I can simply $view->assign('variableName', 'variableValue') No issues there, then in the view if I want to call it, it's as simple as $variableName and it's in the view.

My issue is, I want to be able to minipulate data, for example let's say I want to have a list of items, numbers for the example, a list of 1-10, the user chooses 6, I want a way to be able to "POST" that back to the controller without actually having use POST/GET, I want to be able to essentially let it call an Ajax to send the users selection but I do not know the best way to do so.

If I was doing this without MVC, or Smarty it would be as simple as form, action post, I know that but unfortunately that isnt something I can use in this instance.

Any help you can offer would be appreciated.

  • What do you mean by "without actually having to use POST/GET"? The general way to get data from the client to the server is with some sort of HTTP request. Why do you need to avoid using forms? – Don't Panic Sep 15 '17 at 15:22
  • The idea is that all of the processing is done in the controller - the view then renders the data in whatever format you want (HTML for example). This is then presented to the user. The user then performs the action (i.e. chooses 6) and this then forms a new request to the MVC framework. – Nigel Ren Sep 15 '17 at 15:26
  • ... there's no change using MVC architecture, datas are sended using standard HTTP protocol... then you can use them in your controller... Same thing if you pass them with an Ajax call, you have to set the way you send datas (get or post)... – Jean-Luc Aubert Sep 15 '17 at 15:28

1 Answers1

0

I will begin with the normal workflow, e.g. without the MVC approach.

Let's say, in a certain moment you are seeing a web page in the browser and let's call it MAIN PAGE.

  1. When you are submitting a form from main page without using ajax, the whole page refreshes, no matter if the form action points to the main page or to another one.
  2. When you are submitting values using an ajax call (as part of the main page code), then the main page will not be reloaded. E.g. an ajax call targets ANOTHER PAGE to fetch some data in some format (html, json, etc) and prints the data on screen, in a specified container inside the main page.

Now, let's see what happens in a web MVC architecture.

You must understand, that an MVC application consists of only one page: index.php. This page serves as the MAIN PAGE, but ALSO as the ANOTHER PAGE, targeted when using ajax calls. The index.php page is therefore processed each time when you are sending a request to the web server - be it through manually changing the url in the address bar of the browser, through posting a html form, or through starting an ajax script.

All other components of the MVC structure (classes, template files, etc) are serving only one purpose: to build the structure of the index.php page - as main page or as ajax response page.

So, in principle, in the index.php page you'll have something like this:

  • Read the URL, e.g the url components: controller name, action name, action parameters (HTTP GET query string). For this you can use an instance of a Router class.
  • Create an object of type Request, passing and saving the url components into it. Here are read and saved the other server request variables too, e.g. HTTP POST, HTTP cookies, etc.
  • Create an instance of the View class.
  • Based on the url's controller name instantiate the corresponding Controller class, passing the Request object and the View instance to it. Here you'd give the model layer constructs (like a model factory object) as constructor parameter(s) too.
  • Based on the url's action name call the corresponding controller method, e.g. the controller "action". Exactly here, inside the controller action, are taking place the processing of the server request variables (saved in the Request object), the loading of the template files and their rendering, including transferring the processed server request variables into them.
  • In the last step, the rendered template files will be directly printed or further passed to an object of type Response, which in turn prints them.

In the end, you'll have a fully "constructed" index.php page, which will be either printed on screen by the browser (if index.php has the role of a main page) or processed by the browser as the result of an ajax request.

Note that I used the description of the steps found in the classical MVC approach. There are also other... types of this concept, best presented in Architecture more suitable for web apps than MVC?

Other very good resources:

Good luck.