Jump to content

Mixing Languages


eazyGen

Recommended Posts

Hi guys.

 

This is my first post here - so HELLO.

 

I am a very experienced software developer, but new to web work. I am building a software tool using PHP (primarily). As such, many of my questions will concern optimum solutions (in so far as they may be determined), best practices, and the unwritten standards that will surely have evolved through the PHP community over time.

 

My first question concerns the mixing of HTML and PHP. My efforts so far contain a mix, but it doesn't "feel" right. I am using straight XHTML, PHP echo, and also heredoc. I can post the code if folk think it may help. But what is the general view as to whether "mixing" like this is a good and accepted practice, or is another approach preferred?

 

Many thanks in advance for any replies.

 

S

Link to comment
Share on other sites

For web development the preferred approach is to separate the various components of the application. You can do this a little or a lot. I'm terrible at terminology, but I'll try to give a basic explanation. There are tons of tutorials and resources on the net that would do a better job than I. Just be sure to check a few different sites as not all the info out there is good.

 

Here is a typical structure:

 

Controller File: this file will be used to determine what the user has selected and what they are trying to do (user has requested a list of users, user is trying to add a product to their order, etc.) That file will determine the "logic" file to load to actually perform the action.

 

Logic file, this file contains the code to perform the actions that the user requested. This file may also utilize other types of files (data abstraction layer, classes, etc.) Using a Data abstraction layer provides many benefits. For example, you can change to a different database with little effort. Classes (part of OOP) allow you to centralize the functions for working with specific objects (clients, users, orders, etc.)

 

Presentation: These are the files that create the actual output to the browser. By separating the presentation from the logic you can use teh same logic and repurpose it to different "presentation" outputs: web page, rss feed, xml, smart phone, etc.

 

Link to comment
Share on other sites

Thank you kindly. You explain things well.

 

I am old friends with layered software.

 

At present I wrap the data with classes and only ever access it via published, tested, and proven (OO) methods. If I don't have classes, I would do it with good old fashioned “programs” - in COBOL (a procedural only language) for example.

 

I have been reading up on MVC and in many ways this seems to fall into line with what I have picked up over the years, which is encouraging.

 

If I posit a concrete example, perhaps we could discuss it? (I hope this in no way breaches forum rules).

 

I have a web form – “newcust.php”. The purpose of the form is to capture data so a new customer table row may be created. We have:

 

Name

Address

Country

 

The first two fields are text, and the “Country” field will be a drop down menu pre-populated with rows from the “Country” table.

 

So, as things are with my current understanding, “newcust.php” can contain a mix of PHP and HTML or all PHP (which takes me back to the original question).

 

It will also create a new “Country” object (from the class that wraps the “country” table) which has a “getAll” method. The output from this method is an array, which I use to populate the drop down.

 

The user fills in the from and clicks Submit. I then create another new object (“Customer” - from the class that wraps the “Customer” table”), and invoke the “insertCustomer” method, which inserts the new customer row (let us say the key of the table is an auto-increment “id” field which means SQL will take care of it).

 

This seems elegant enough and would work reasonable well.

 

The question I have is:

 

“Where does the 'Controller' part of MVC come in'.

 

Apologies for the length of this reply and many thanks in advance for any response.

 

Steve

 

P.S. I have done a lot of web crawling thus far, but I do find forum discussion useful, over and above standard research.

Link to comment
Share on other sites

I made an error above but cannot seem see how to modify the text  :confused:

 

The error was that when the user clicks submit, a validation script will run and, if all ok, this will invoke the "createCustomer" method on the customer object.

 

I think.

Link to comment
Share on other sites

I don't use MVC, so I can't speak to that. But, using your customer form as an example, this is the process I would use:

 

The user points their browser to newcustomer.php. We will call that the controller file for this process. Typically, I just set some parameters in the pages that the user accesses and then include a "master" controller file, but this will do.

 

OK, this controller file will first do a check to see if the form has been submitted. If not, it will include a separate file that has the actual HTML form (with maybe some minor PHP logic). And then it ends. However, if the form was submitted the controller file can then validate the form data and attempt to create the client. When writing procedural code I will, of course, do the validation in that page before attempting to create the new record. But, if I am using a class I will do most/all of the validation in the class. I would just pass the variables from the form post to the class method to create the customer. If the method determines that there is an error it will generate an appropriate error message back to the controller script.

 

OK, no matter how I do the validation, there are two possibilities. Either validation fails or it passes and the record is created. If validation fails, I will again include the form page. Some of the logic I spoke of previously would repopulate the form with the values the user previously submitted and also display an error message as to what needs to change. If validation passes, then I may do something entirely different. Perhaps I display a confirmation page or I might display a summary list of customers. Either way, the controller script will determine that the customer was created and include the appropriate page to be displayed (which might also be a controller script).

 

One note about the form page. I could also use that page for editing purposes. That is why it may include some logic. Based upon the parameters set it may need to include a hidden field for the customer ID begin edited and there might need to be some minor text differences on the page based upon whether the user is creating or editing.

 

I made an error above but cannot seem see how to modify the text

You can only edit your posts for a certain amount of time after creating them

Link to comment
Share on other sites

Thank you for your thoughtful reply.

 

A first impression question - would it be true to say that the controller scripts could get pretty large (in relation to the size of app) quite quickly? If so, would you try and separate out "related" control scripts into more than one controller?

 

Thanks again,

 

S

 

Link to comment
Share on other sites

It is generally considered best practice to have fat Models, and thin controllers. The more you can move into a Model the better as this means it can be used easily in different locations.

Many thanks.

 

So you would create a class for each table (a model) and place in there, not only the data access code, but also as much of the validation that relates to the table as possible?

 

S

Link to comment
Share on other sites

It is generally considered best practice to have fat Models, and thin controllers. The more you can move into a Model the better as this means it can be used easily in different locations.

Many thanks.

 

So you would create a class for each table (a model) and place in there, not only the data access code, but also as much of the validation that relates to the table as possible?

 

S

 

Precisely. The Model should be kind of like a gatekeeper of your data. It makes sense that it would be the place to store your validation is this then ensures that the same rules are applied everywhere within your application.

Link to comment
Share on other sites

A first impression question - would it be true to say that the controller scripts could get pretty large (in relation to the size of app) quite quickly? If so, would you try and separate out "related" control scripts into more than one controller?

 

To elaborate on my previous statement

The user points their browser to newcustomer.php. We will call that the controller file for this process. Typically, I just set some parameters in the pages that the user accesses and then include a "master" controller file, but this will do.

 

I may have a "master" controller file, but that does not mean it is my only controller file - it is simply the controller file that is first called. that way I have one file to put/include configuration data for my site. As an example, let's say my application has, among many different section, an administration area. In that area user's can work with different entities such as users, customers, products, etc. Then for any different type of entity there are different operations that can be performed. So, when the users selects a link or clicks a button.

 

Ok, so now let's say a user brings up a page of products and clicks the link next to one to edit the product. That url may look something like this:

mysite.com/admin/edit_product.php?id=123

 

Now, in that page "edit_product.php" I will set some variables for ($module='admin', $type='product', $action='edit', $id=123). Then I will include the "master" controller script. That master controller script is only looking for the $module variable. If it matches one of the available modules it includes that module controller script (else it includes the default, i.e. home page). Then on the controller file for the admin module it will check for the $type variable to determine with record type controller file to include and that page would perform the actions necessary based on the 'action' variable. This can go as deep or as shallow as makes sense for your application.

 

Of course, this is my methodology - that works for me. Not saying it is the only way or the best way.

Link to comment
Share on other sites

Thank you - this is helpful.

 

As a consequence of this discussion I will build an example structure and place it on my web site. When I have it complete, I will post back and we can perhaps discuss the matter further?

 

S

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.