PHP generating html [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am new to PHP, coming from ASP.net background, the most trouble I am having is generating html from PHP. My question is how is it done in industry? I see 4 ways:
generate from inside PHP program.
use PHP "helper classes" to do that.
mix PHP from inside HTML.
use templates.
Are there more ways, and which one is used on professional projects. If it number 2 or 4, could you suggest "best" (most used) helper classes, or templates.

Use an MVC framework like CodeIgniter, and follow the standards there. Try to keep your business and display logic as separate as possible!
The reasoning behind this is that if you decide later on to modify your code, it's easier to find the code in question. It's simpler to search through a few template files than it is to pick through all your logic to look for one snippet of html.

Most professional development is now done using an MVC framework, such as CakePHP or CodeIgniter.
You could also look at a standalone templating system like Smarty.

When you start with php, you find several ways to generate your webpages.
Html is a template language, so it is just formatted string, and php is very good generating string, so you have a lot of ways to do it.
But some ways are better than others.
For example: If you mix your html code with your php code, you get a very difficut to read, mantain and scale code, so it is better if you separate the code in layers. MVC is a design pattern that handles very good this kind of layer abstraction, so read a little about it.
Inside PHP, you could find several tools to do it at the good way, like using a framework for your project. The frameworks make easy and quick your development and manage the mvc abstraction well.
I recommend Laravel, it is easy to use and is so powerful, that you will do your job really fast.
But if you don't want to use a framework, you still could separate your logic from the html code, and fill the html only whe you need. Look at The following class, maybe it could be useful to you.

Related

It is best to generate the html content through php classes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've been playing around in the Web Development field for 2 years now and I am pretty proud when it comes to my progress as a programmer looking back at my humble beggining. This is not very relevant but I've wanted to back up my question with some background story. What it comes to my interest during a conversation with a friend was how to best structure your code when it comes to a website so it wil be very scalable for further modifications using an oop aproach. He suggested at the end of the discusion to use php classes to generate the html content. I personally use it like this only when the php helps me to retrieve something server-side related. I use HTML, CSS and JavaScript (in that order) as much as possible before calling in PHP. That's how it should work, in my opinion, especially when you have to work on a presentation website for example. What's intrigue me thinking about my friend proposal is that I structure my code that way using php clases to echo html content parts like header, menu, forms, slideshows, footer etc. will indeed scale my code way better and help my programming skills progress. I should end up with an index.php that returns objects of the respective classes creating that way the desired html content. This is how I image things working and I am asking you to help me reach a decision. I have some upcoming free time available and I want to invest it in becoming a better web developer.
It is generally a bad idea for all HTML to be generated this way.
In professional web development you often have front-end developers and designers whose only responsibility is the HTML/CSS/assets and/or JavaScript. The backend developers, (the ones writing PHP code), are usually responsible for the business logic of the application.
By having PHP generate HTML you are violating separation of concerns principles. Things will get messy. It's harder to scale up, and you will lose the benefits of being able to use a HTML editor.
Generating HTML serverside dynamically has uses in very specific circumstances. It can be done, but just because you can, doesn't mean you should.

Startup building and mokup [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
since about two years ago, I did find my interest in code (Hardware/Sytems/Web) and now, I've found a project which motivates me a lot (It takes all my free time indeed).
Starting this point and because my project could soon switch from a free time project to a daily job, I'm currently developing a mockup of this project based on PHP/MySQL and JQuery.
Even if I'm a true Python/MongoDB lover and a System Engineer, I did prefer those technologies to build up my mockup because of their simplicity to build a complete functional private stack at home.
I'm pretty advanced on my mockup and it seems to work as I want it.
Now I'm wondering if, about your point of view, would have been better to start to build my mockup using directly the targeted technologies (Python/MongoDB) rather than to use the easy PHP/MySQL couple to do it?
Obviously, because I plan to made this project my daily job, I had to have something visually functionnal to be able to raise a little bit of money, and about me, using an easier stack it's more easy, but I would like to have your feedback on this kind of question.
The idea that PHP/MySQL is easier or simpler than say Python/MongoDB is just inconsistent.
If you compare for example, Django (the most popular python web framework) with symfony(PHP) you will find that they are almost identical in terms of features and architecture (symfony is actually slightly more complex but also has more very advanced features).
For mockups, if I were you, I would use solely HTML/jQuery/CSS.
Build your pages just like you would like to have them in your beta version, use jQuery to load sample data written in json.
That's all you need. You can even find WYSIWYG application to speed up the process.
Later on, you can build the back-end application using either python or php, it won't matter.
The integration process will be identical, create your models, create the controllers, and use the HTML you already have as templates.
Building your app in php/mysql then convert it to python/mangodb will make you rewrite almost all the code simply because python is so much different from php (easier I would say too, but that's just my opinion) and because mangodb is not a relational database meaning you will have also to rethink partially your architecture.

Is there a disavantage in rendering a pure html, css and a client side script like JS? versus php rendering a web page? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Most of i have read in book and tutorial most recommend that you render html,css and Js in Php.
Is there a disavantage in rendering a pure html, css and JS in a web design project?
it seem really very long file if i use Php to render a webpage versus html.
We are talking about milliseconds here. Unless there's some really heavy
load on the server or some very complex scripts running, the users won't
be able to notice much of a difference. The page creation will still be
much faster than the transfer to the browser.
PHP is a server-side language and the use-case is a bit different. If you want to use PHP in your project, then use it. For example, if you want to query the database, get the output or do similar operators, you'll have to use PHP. Pure HTML alone can't do that.
It seem really very long file if I use PHP to render a webpage versus html.
The length of the file is irrelevant here. It's the use-case that matters.
To answer your question, there's no harm if you create the page in pure HTML / Javascript alone but this may not work for some particular situations. There are occasions when pure HTML / JS alone can't do what you want (for example, querying the database). In such cases, use PHP. If you don't need to perform such actions, don't use it.
Yes it will really make your code longer. However, you can use PHP framework which uses MVC design pattern that will help you separate your views from your logics. They also have template engine so instead of writing php codes in your views you just use brackets or {{ }} or whatever they use.

Benefits of using cakePHP? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I know PHP well. But i haven't tried cakePHP before and i was willing to learn cakePHP. So just want to know that is there any benefits of using cakePHP over PHP?
Thanks,
aby
cakePHP, like CodeIgniter, FuelPHP and Symfony (just to name a few) are frameworks. What this means is that they have tried to abstract some of the normal, every-day things you do in development in an effort to speed up development time and make you more profitable.
In today's world, there is no excuse for not using a framework. If you refuse to use a framework, you find yourself creating a lot of the things that a good framework provides anyway (i.e. data abstraction layers, session classes, form validation, etc). By using a framework, you not only gain time, but you gain stability as the different components of the framework should be designed to work well together and have been thoroughly tested by the community.
Whichever framework you choose is up to you and hopefully now you understand it's benefits.
CakePHP is an MVC framework that will allow you to setup your data model and connect it to a database to be easily manipulated in PHP. It also allows you separate your business logic from your display logic using a controller.
It also gives you a lot of useful functionality like authorization, sessions, form validation, pagination.. for free. You should check out the feature list.
If you didn't use CakePHP you would have to roll your own framework, which is a lot of extra hassle and work. There are of course other frameworks.
The answer is pretty simple, it comes down to your need's, do you need a framework?
You say that you know php well, i doubt you know it as well as you think as you would not ask a question of this nature, you would understand the concepts behind a framework.
You say:
So just want to know that is there any benefits of using cakePHP over PHP?
it's not a language sir, it's a framework that supplies the PHP foundations for an application, why would you not wan't to use such a thing, what you should be asking is:
What's the best framework to learn once you have mastered PHP Fundamentals.
But don't ask it here, there are plenty of posts that answer that question for you.
CakePHP is an MVC framework But to simply say you need to design the data models and relationship (creating necessary tables) before start the work , once you use bake command 70% of coding are given

Lightweight MVC - no framework [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Hi sorry if this is a naive question, but what did people do before mvc frameworks became so popular? All you hear of nowadays, and im talking php here, are mvc, Zend etc but what did developers do beforehand?
Are there some developers who use the mvc pattern but without a framework - if so how do they do this and is it really complicated to set up?
MVC is a design pattern. You can easily roll your own MVC "framework" (technically even without using object-oriented programming). The main goal is simply to have a separation between data storage, business logic, and presentation.
When I was first learning about MVC, I decided that trying to sift through the mountains of code of CakePHP or other frameworks was simply too complicated. I started writing my own "framework" using this tutorial (http://fuelyourcoding.com/php-frameworks-just-roll-your-own-part-1/). It's really not as much work as you think (you can go through that tutorial in a day and have a very nice mini-MVC), and you can expand it later into a full-fledged framework later on if you have the time and dedication.
As to the question of what developers did before frameworks, well, they just wrote everything themselves. Unfortunately this led to a lot of spaghetti code with HTML mixed with PHP blocks and SQL statements, but that's not really a fault with not having a framework, rather with not trying to implement any sort of separation of concerns.
Edit: Part 2 is probably the most important part because it shows you how to make a template. This isn't actually the exact tutorial, the one I used seems to have disappeared.

Categories