Download Ajax for complex projects

My problem is not really in ajax loading itself, but rather the ability to download it without javascript. I mean, I can easily handle it when I code the entire project based on ajax accessibility OR without using ajax.

// EDIT: although Arend already had a more or less correct answer, at the same time "there is no direct answer to this question." However, I would like to see some other developer approaches for scripts like mine! Even a few links can help!

Basically, I'm just upset by coding all two times on the same page to make sure that both users without Javascript enabled have the same experience. This is annoying, and I was always wondering how others solve this problem.

When I update, for example, two divs depending on the same variables, it becomes messy. Here is an example:

non-js version

require 'classobject.class.php'; $another_var = 'something'; $class = new classobject($_POST['variable']); // just an example to show that this is dynamic - I'm aware of injection! $function = $class->returnsth(); // returns 1 if(isset($_POST)) { echo '<div id="one">Module 1 says:'; $require 'module_one.php'; echo '</div>'; echo '<br /><br />'; echo '<div id="two">Module 2 says:'; $require 'module_two.php'; echo '</div>'; } 

Now in module_two.php and module_two.php , I have code that runs differently depending on the function variable returned $. How:

 if($function >= 1 && another_var != 'something') { // do stuff } else { // do other stuff } 

Now that this works easily with rebooting, when I want to load two modules on keyUp / enter / submit or something else, I have basically a few problems:

  • I need to send $_POST variables manually to modules to use them.
  • I need to re-execute the class and its methods and make a link (require_once) to it in each of the file modules.
  • Since $ another_var does not exist in modules, I will have to send this variable to each module too (with a message, for example), and then, before it can be used, I will have to “change” it as $another_var = $_POST['another_var'];

I find it a little annoying, and I wonder how you guys do it. I hope my coding method is not too stupid, but I can’t think of anything else. This is probably difficult to relate to my simplest example, but there would be too much to implement the entire project with code. To summarize, I am looking for the best way to code and clear this mess - there must be a way! I was thinking about sessions, but for compatibility, I don't want to rely on either (unless someone permits cookies).

If you cannot relate to what I am trying to execute so that my code is compiled, I will explain the scenario that I encounter quite a lot (it doesn’t matter if you already understand my misfortune ):

  • Basically, I have an index.php page where everything is executed, with the html body and css-style and so on. Some variables are expected on this page, which are set from the page for which the index is required (for example, $another_var in my example).
  • Now other variables can also be set (for example, from a form). Depending on the fact that different classes and methods load new variables (arrays) that are used during loops in my modules to completely discard everything.

Hope this is not too abstract. Think of a reservation system in which some variables are set on the page from which you are leaving (the event you want to book), and then a few more things are set by the user (time, some preferences, ...). In the end, it should show the results from the database to the final result - you can say that the user narrows the results from step to step.

+6
source share
2 answers

There is no direct answer to your question, but there is an idea for reflection.

The difference between the problems You might think if you can separate the logic of logic and layout logic. Often, using a template engine can greatly help with this. I had positive impressions, for example, Twig or Smarty (it was some time ago, I don’t know how they are measured now). This requires you to write your code (less linear), but more logical.

A typical example of OOP, as a separation of concerns, might be something like this:

 $this->setParam('Myparam','myvalue'); if ($this->isAjax()) { $this->setTemplate('ajax.php'); $this->setLayout(false); } else { $this->setTemplate('normal.php'); $this->setLayout('Mylayout'); } return $this->render(); 

This is an imaginary situation that can be found in many MVC-like applications and frameworks. The basic idea is that you should be able to separate your layout from your data. I would suggest taking a look at some modern frameworks for inspiration (e.g. symfony, codeigniter, zend framework).

Glossary / Commonly Used Concepts in Unleashed PHP Applications Here is a short list of concepts that you can use.

Mvc example in php: http://www.phpro.org/tutorials/Model-View-Controller-MVC.html

Note. I don't really like the implementation. I much prefer the existing framework. I like the whole explanation of this lesson. For instance. for me, this link is for learning, not for implementation.

Silex For a simple decoupled php microstructure, I would recommend silex for make symfony2. It is easy to implement and learn, but contains the basic concepts described here; and uses all the useful features of php 5.3+, such as namespace and closure.

see http://silex.sensiolabs.org/

Figure of the control panel . You have only one and only entry point for your code. I usually only have one, and only one item in your application. Typically, the frontcontroller sends a request to the rest of the application.

http://en.wikipedia.org/wiki/Front_Controller_pattern

Routing

A routing system is often used in conjunction with a frontcontroller pattern. It basically describes which URL is connected to the module / controller. This allows you to change how users access your application without changing URLs.

See: https://stackoverflow.com/questions/115629/simplest-php-routing-framework

controller

A controller is a place where buisness logic is applied. Retrieving data from a database, checking privileges, setting up a template, setting up a layout, etc. (Although this also moves outside the controller if it gets too large for a single issue).

Model The model is basically a layer in which use manages your database. It can be a simple class in which you move all your mysql_ * functions, or it can be a full-featured ORM. The basic philosophy is that all the logic associated with the extraction and placement of information in the database is shared.

One step up: ORM Relational object models, these "cartographic" SQL records for PHP objects, are a frequently used method in applications. Doctrine and Propel are two of these well-designed libraries. I rely heavily on these systems in my development. In this sense, part of the doctrine or propeller will be a model layer.

View: A view usually consists of a template engine. Some use simple PHP as a template, others, such as symfony, create a separate area in which variables are placed. There are many discussions and opinions about which is better, one of them is here in stackoverflow:

I like: - Twig: http://twig.sensiolabs.org/ - sfTemplate: http://components.symfony-project.org/templating/ - Smarty: http://components.symfony-project.org/templating/

Decoupling mechanisms:

  • Event Based Systems Using events in yours can help split code. For example, if you want to send an email after saving a record, events are a good solution to this; in general, the model does not need to know about email. Thus, events are a way to connect them: you can let your -email-send-class listen on certain entries so that they can send the correct email address. (Perhaps you would prefer your emails to be sent from your controller, this is probably a matter of taste).

  • Injection injection When using OOP code in PHP, many relied on single-element classes (configuration, etc.) running around. From the point of view of OOP, this can be considered bad, because it is difficult to test it, and it is not considered very elegant to have dependencies on it. Dependency Injection is a pattern that has appeared in Java form and is now used in new environments to get around this. It may be a little difficult to wrap around you, but you will see that it is returning in a few new frames.

Including dependencies in php: Injecting dependencies in PHP 5.3

Frames:

Many of these methods are complex or a lot of work to implement. Many will live within this. You might need a framework. You may or may not want to have a framework; this is your choice. But it’s still useful to find out how frameworks do it, and not try to reinvent the wheel yourself.

Frameless frameworks: https://stackoverflow.com/questions/694929/whats-your-no-framework-php-framework

Good habits: https://stackoverflow.com/questions/694246/how-is-php-done-the-right-way

Frames worth seeing (imho): CodeIgniter, Kahona, CakePHP, Symfony (1.4 / 2.0), Silex, Zend Franework, Yii. They are much more than their loyal fans and haters.

+11
source

I wrote something like this with PHP. I have already analyzed the rendering of each page to determine the $content variable, and then require('layout.php') . The $content variable is just a big line of HTML.

I wrote a PHP function to determine if an AJAX request was or not.

Non-AJAX answers display a layout with $content in the middle, b / t header and footer.

AJAX requests basically get the following: json_encode(Array("content"=>$content)) . And I use jQuery to get the HTML from the JSON response and change the DOM. Using json_encode() will handle the escape string for javascript.

After all, I actually have AJAXified on every page without overly designing a complicated solution.

Any browser that supports AJAX can also open the link in a new tab / window to simulate a request other than AJAX. (Or add a link / share the link.)

0
source

Source: https://habr.com/ru/post/902460/


All Articles