r/PHPhelp • u/[deleted] • Jan 26 '23
Solved Back to PHP/HTML/CSS
Hi :)
A long time ago I was using PHP5 + mySQL for some simple projects. After years I want to back to PHP :)
The question is:
- what's the best practice for web pages now?
- how the web structure should look for best performence? (include parts of html etc?)
- I want to build some dyamic page which will download data from other site to my DB etc
7
u/kAlvaro Jan 27 '23
I think the main change is that the PHP as HTML templating engine paradigm has been mostly discarded. Work in terms of including files with dynamic blocs into your HTML does not scale nicely, and both the language itself and the third-party libraries ecosystem have evolved vastly and now provide much better solutions that make it easier to build complex applications, but also work for simple sites.
From here:
project-root/
html/
css/
js/
img/
inc/
lib/
index.php
products.php
forum.php
contact.php
about.phpTo here:
project-root/
vendor/
app/
lib/
var/
log/
public/
css/
js/
img/
index.phpThese are fictional layouts, but any modern framework you choose will provide a variation of this. The basic idea:
You only expose a single PHP file to be executed from the browser, as well as public resources like client-side JavaScript, CSS (possibly generated from SASS), public images... Everything else is hidden from view, one directory up the web server document root.
You define entry routes, as in:
GET /customers GET /profile-{id} POST /ordersRoutes are processed by specific methods and classes.
Output is rendered into a template.
1
u/joshcam Jan 27 '23
^ Best answer to your first two questions right here. The last one was a statement..
1
u/oldschool-51 Jan 27 '23
Well, considering that most of the world's websites are on Wordpress - which is php and mysql - the statement "discarded" is, ahhh, overstated. Its true that for apps that need massive scale there are other approaches, but for normal corporate data-driven apps, php is still the way to go. Particularly with PHP8.1+ things are much faster but need a bit more care in coding as PHP8.1+ is pickier about undefined index keys etc.
1
Feb 02 '23
Thanks for all your answers! I had few hard days so at weekend I can sit, read and work on my page :)
4
5
u/williarin Jan 27 '23
Use a framework, such as Symfony or Laravel. Your last point could probably be a CLI command, frameworks have that too.
2
u/ZippyTheWonderSnail Jan 27 '23
There are two paradigms on much of the modern web.
The first is a basic MVC (model-view-controller). It serves pages directly from the web host. Generally, the database is hosted separately on Amazon, Azure, or some other host good at keep them secure.
https://www.giuseppemaccario.com/how-to-build-a-simple-php-mvc-framework/
The second paradigm is the API driven site. A page template is delivered from one host, while the content is populated via AJAX from a PHP based API (hosted elsewhere or on the same site as the template).
https://code.tutsplus.com/tutorials/how-to-build-a-simple-rest-api-in-php--cms-37000
Choose which to use is based on the kind of site you're making. A brochure ware site is very different than a web app.
First decide what a site should do, then start with a simple MVC. Move onto a framework. Then make your first API. It won't take as long as you think to get back up on your feet.
2
u/ardicli2000 Jan 27 '23
Since I do not have much experience with MVC, I have opted to write my own API, and rendred pages with an AJAX request on a SPA. It works wonderful.
But if you do not structure your app well enough, things can get messy easily. and it is hell of ajax calls everywhere. ı can still seperate some calls based on requested pages but it is not ideal. Performance wise it is very good. But it is real PITA to make it SPA.
For future compatibility and ease of maintenance, I would suggest MVC. You can start with a lightweight framework like CodeIgniter.
2
u/equilni Jan 27 '23
The first is a basic MVC (model-view-controller).
The second paradigm is the API driven site.
You can still have an API driven site using MVC architecture. The tutorial you linked does just that.
1
u/ZippyTheWonderSnail Jan 27 '23
Absolute correct. As I noted, the page templates can be served from the same site as the API. It is a hybrid approach popular with sites which need the main content to be available immediately, while less relevant data can be loaded as the API responds.
1
u/baohx2000 Jan 27 '23
your last bullet is just ajax, which is 99% of web applications at this point, unless you're crazy and decide to do server side rendering like we used to with PHP. If you're doing a web application (not a public website) then you'll want to start looking into front-end frameworks and libraries. Front and back ends are mostly separated. Back ends are all just web APIs of some sort (please don't dive into the graphql hype immediately, start with REST basics).
For public websites, not much has changed. It's all about speed so you want as much content as static as possible (not served by dynamic services like PHP), then load any dynamic bits later.
7
u/32gbsd Jan 27 '23
Web pages are still html, imagss and js.