Quick question for anyone who might have some input.
Normally when I have php/html blocks of code that get used on multiple pages (nav bars, footers, links, etc.) or when I use AJAX to return parts of a page, I assemble that bit of code in a separate file, then include it on the main page as well as in the php insert page to send back with AJAX return data. This works fine.
I've been experimenting with using the same concept except just wrapping my html (which includes php variables) inside of a php function in a separate file, then calling that function where I want it.
If there is a best practice for this, I'm all ears.
There are many ways to handle this, but I think you'll generally find it easier to make a sort of API layer that just returns the data (often serialized to JSON) over AJAX rather than parts of the page.
This separates concerns, allowing you to maintain your API services independently from your pages. It also allows caching of your pages.
To actually handle the updates client-side, there are a number of frameworks to choose from. I've been using Vue.js lately myself. There are also JavaScript-based template engines you can use to render the parts and insert the data into the page.
Related
I am embarking on part of a project now that has me planning on how to load a dynamic table data from a DB. I have uncovered two basic methods.
I believe that I can use url query strings to communicate with the php backend of my phpbb3 forum. And it can load the appropriate data and ship it off to the user in full static page chunks. So I would have something like /stats.php?page=3&orderby=name&dir=desc.
Or I can just send the same empty page to everyone and the browser can dynamically load anything that the user wants using ajax.
Or some combination of the two.
What is best practice? What are the downsides and upsides of both?
It really depends what you're trying to do. For simplicity's sake, I would say that the first option (just load it with the appropriate query string variables in the URL) is better.
Rendering a page using AJAX is pretty much always more complicated. However, it also gives you much more control over the UI if you know what you're doing. From my experience, if you want your page to be more like a "web app" with dynamic things happening everywhere, it is much easier to simply load JSON data from the server via AJAX and to dynamically create views via some sort of templating system. Otherwise you're stuck with loading the DOM with PHP, and then somehow communicating that data to your JavaScript, either by using data-XXX attributes on DOM elements, having PHP output a JSON string at the top of a page and assign it to a JavaScript variable, etc. It could get very complicated and convoluted.
In your case it looks like you're just trying to allow users to view certain data from your forum. Barring any additional requirements, I would recommend going with the first option because it will be much easier. This is simple enough that you don't seem to need to load anything dynamically.
A good rule of thumb is the more complicated and dynamic your UI, the more you should think about moving to a "web app" framework, and just let the server act as a REST server.
I would like to know if it's possible to make pages invisible for users with .htaccess or something else.
Some background info and some other questions :
I'm building a web application which does a lot of dynamic content loading. At the moment i'm just doing ajax calls on a ajax.php file and i give a POST parameter with the name : action. Depening on this action I call a method of a class, this class then echo's JSON or HTML.
But i'm starting to get at the point, that i have functions who return almost a complete html page (SQL and php in between) which kinda looks dirty. Is it a better option to build seperate .php pages for this content, and then load the content just from that page? (I use Jquery, so that would be the .load function as call back)
The problems i see with this method is that people can find those .php pages and access them, and then see information that's not relevant and has no lay out or something. So i want to block these pages. How do I do this with .htaccess?
Now if u think i'm doing stuff completely wrong, just tell. A basic situation in my implementation :
Fill in a client number in a search form -> press enter -> ajax call with post parameters action : getclientinfo , id : the value in the field -> ajax.php gets the correct class and method -> .class.php executes his method and checks if the input is ok, if so it constructs a page with content from the database -> All this gets returned as JSON something like :
{ success : OK; content : the complete content }
-> then with jquery i check the success boolean, and then load the complete content into the current page.
Is this a good method, or am i doing things bad ?
The thing i'm not feeling myself ok with, is the ajax.php page. What's usually done ?
Thanks in advance. I got many answers from this website, first time ever I'm asking one myself.
Seems like you have two problems: access control and organization.
It'd be best to lock access to pages with a rudimentary authentication level programmatically (in PHP, using session data). Otherwise, the most rudimentary form of access control is to restrict with apache's .htaccess file, but, rules for query string / post can be confusing. See this example: Is it possible to redirect post data?
Since that kind of locking down is complex, it might be logical to dump AJAX and render full, complete pages instead.
In either way, what you seem to be conceptually illustrating is a need for separating of content and logic. This is good. However, it's time to take your development process to the next level.
Rewrite your code to where only logic and data querying takes place in PHP files. Secondly, output all HTML in your pages by using the Smarty library http://www.smarty.net/docs/en/
The Smarty templating system is pretty extensible. It has inheritance capabilities so you can produce page variations, template includes, and otherwise run circles around HTML. A handful of Smarty statements can make your life so much easier.
How do you go about designing/structuring a completely ajaxed MVCish style site? I guess what confuses me the most is that there would only be one view that adapts and changes to the user actions...
I'm looking to build a really simple app, both front end and back end in this style.
Yeah, you'd be building a "front controller" of sorts that would route the views (there would be more than one) to the page for rendering in divs. My company does this with two of its apps and it's lightning fast and a great way to go for a simple app...users love it. Key to the success is a well-formulated layout with well-defined divs to receive the content. As you'll be repeatedly writing and re-writing to them, you have to ensure ahead of time that they are able to handle data of various sizes and amounts as you'll have very few ways to effect layout per page on the overall container....besides after-the-fact hacks (just say no!)
Do as you would with controllers and models and the views (the front-end view code) In some cases our code will use an intermediary page (we call it a mid-model) to generate Jquery data in a JSON string format. It's not quite a model in that case, as it takes action like a model but sometimes returns more than just JSON depending on the needs of the Jquery element.
An interesting offshoot of this system is the use of a program called XAJAX. It's a PHP library that facilitates AJAX called directly to PHP functions, so it eliminates the need to do intermediary JSON generating pages like Jquery uses. For those who understand PHP but struggle with Javascript, this can be an easier solution to grasp. While the documentation hasn't proven very strong, it's a very powerful tool.
I would do all your views as usual (except they would only be HTML fragments that would be inserted into the page) and then have an extra "special" controller and view that loads your views via Ajax. When a link is clicked or something like that the JavaScript makes an Ajax request to the special controller with the view you want to load. The special controller then renders the view and sends it back to be inserted into the page.
I've been trawling SO for a couple of hours tonight looking for some answers, but I've not found anything that really answers what I'm after. My apologies if it has, in fact, been answered already.
I'm designing a new website and I'm trying to decide on the architecture to use to serve the content. In the past, my websites have used PHP feeding data into Smarty templates.
However, recently at my work, I've been working on a Java web application where jQuery was used to retrieve the data from a RESTful API (which returned JSON), where HTML template pages were used as the base and javascript was used (utilising jQuery) to fill in the content.
My question:
The website I'm designing will be in PHP, but would it be better to construct (or use an existing) RESTful API or to continue on as I've done before feeding the data into Smarty templates?
Are there real benefits to one or the other, or does it just come down to developer preference/experience?
If it helps, the website will be for a church, where the content types will be CMS-like; news/announcements, wiki-like pages, and a limited type of social networking (for the minister to communicate with parishioners).
Short answer: It sounds like filling the content with JavaScript would not be useful in your case. Loading the data with JavaScript is adding a layer of complexity with minimal or no benefit (in your case). Take a look at CMSs and websites that have similar functionality to what you're doing. WordPress, Drupal, etc.
For an example of when it might be useful to load data with JavaScript, check out the tags section on this site. When you search for a tag it queries the server without reloading the page. However, the initial tag list is loaded during the initial page load without any JS.
Here are some cases where you might benefit from loading information with JS:
There is some data that will take longer to load. For example, you're getting data from a web service. Using the traditional method, you would need all the data you need for the page to be available before it can be displayed. If you loaded that data with JavaScript, the page could load and the slow data would appear when it returns. Realistically, you would probably just cache the data but this is just an example.
You will be getting more data as the user stays on the page. You might want to update the page without refreshing.
You want the user to be able to query more data without refreshing the page.
Your users have limited bandwidth (mobile).
This site has more guidelines: http://www.oracle.com/technetwork/articles/javase/index-137171.html
I've been tasked to migrate a web application to a more 'modern feeling' AJAX web 2.0 deal. The application as is currently uses PHP to pull data from the database, present the user with forms, and then update the database based on those form submissions. Frames are used to have a persistent main navigation menu, and a content area where the actual
So each php script basically looks for $_POST information; if there is none, it shows the user database data, otherwise it updates data ( provided it is proper data ) and then show the user the result. There are simple get navigations that show subsets.
To migrate this to a AJAX site with a css layout with the content changes happening inside a div, I'm precluded from using POST, because that refreshes the whole page, right? ( I mean I could, but that would be wasteful -- I don't need to regenerate the whole page when only a small part changes.) So basically, the whole task is using Javascript to read the form information, send an XML HTTP Request, and display results? That sounds like a lot of re-writing the existing php funcitonality in javascript, which I would hope to avoid.
Have I understood the task correctly? Are there libraries or frameworks that can help me?
You have two problems here, which are related in some ways, but shouldn't be simply lumped together.
CSS for layout
Only loading part of pages when forms are submitted
I'd work on the separately (while keeping the other in mind as you do so)
First, I suggest you focus on moving to web standards based pages — without introducing Ajax.
It is true that there are some inefficiencies to be had when reloading the whole page, but this approach is simple and relatively easy to debug.
While you do this, consider separating out your display and business logic. The MVC pattern works well for this.
CakePHP is a popular MVC framework that might help.
Once you have a working system, then you can worry about using Ajax. Follow the principles of progressive enhancement.
If you have separated our your display logic from the business logic you should find it relatively simple to reuse your existing code with a different View that provides the data you care about in a JavaScript friendly format (such as JSON).
You can process this to update the parts of the page you care about.
Frameworks that can help you include YUI and jQuery.
I wrote a simple example last year. Lines 51 onwards of the main script pump data into either an HTML template for processing directly by the browser or a JSON module for processing with JS. There isn't a great deal of duplicate effort there since the code for looking at the parameters sent by the user and extracting data from the DB based on it is shared.