Jump to content

Is there a way to not call each file in index.php?


Recommended Posts

I've inherited a website and I'm no PHP expert.

 

The way pages work is the urls are displayed as follows http://www.website.com/?action=<page_name>

 

The pages are stored in a folder called templates as .tpl files. Amongst these is a file called layouts.tpl which is the website layout and menus, inside this there's a php include

<?php include("template/$content_for_layout.tpl"); ?>

which basically draws the content for the page from each static file when requested. In the index.php each static file has to be included as in the following example:

 

case "conditions"; $title = "Terms & Conditions";
$content_for_layout = $_GET['action'];
break;

 

Without this in the index.php the page just reverts back to the homepage when you enter the url. I personally don't think this is right? It is handy to be able to manage all the page titles from a central location (index.php) but seems quite messy and cumbersome. I plan on making an articles or news folder and regularly creating pages and if each one has to be relayed this way in the index it is going to get big and cause headaches.

 

Is there any way around this that anyone can see?

 

 

 

... On another note. If you type in the actual physical url of the file location it shows the html of the page without the layout i.e. http://www.website.com/template/conditions.tpl

 

I don't know how common it is for websites to display "../?action=pagename" or similar but I wouldn't have thought it would be ideal for SEO purposes?

 

This is actually the preferred approach. This approach has several advantages. You can have pretty URL's, you won't have duplicated code, bugs can be easier to track down, it can prove a little more secure, etc. Here is a diagram of how CodeIgniter (and many other applications) route the application.

appflowchart.gif

action=pagename is better for SEO, because you can have descriptive pagenames that are easier to read.

 

However, I don't know why your code has this bit:

 

case "conditions"; $title = "Terms & Conditions";
$content_for_layout = $_GET['action'];
break;

For one, it's not valid PHP.  Is this switching on the existing $_GET['action'] variable?  If so, why does it also set the title?  Shouldn't that be in the template itself? 

 

This set-up looks about 80% correct, which is probably why you think it's all wrong.

case "conditions"; $title = "Terms & Conditions";
$content_for_layout = $_GET['action'];
break;

For one, it's not valid PHP.

 

He said they are .tpl files, so I'm thinking he has some sort of template parser.

He said that part was in index.php

I don't know why the title is determined in the index.php... there's a config.php page (which is 'required' by index.php) which has a title variable this seems to control generic titles across all dynamic pages/pages without titles. If I remove the $title = "Terms & Conditions"; from the code then the title reverts back to this generic title. The layout page has the head tags so the content pages wouldn't want to repeat these I'd imagine, even if I try to add a head and title tag to the content pages it doesn't show... the code used for the title in the layout is:

 

<?php require_once("aper_consts.php"); if ( $_SERVER['HTTP_HOST'] != 'testing.local' ) echo ($title);?>

 

The layout is a .tpl file as is the content that go in the middle of the layout. But if I don't put in the "case "conditions";..." into the index.php then it shows the homepage rather than the page it should display, which I think is related to this line at the end of index.php:

 

default: // homepage
$content_for_layout = "homepage";
break;

 

 

The way the pages are loading I think it loads the layout.tpl with it's associated title and meta tags then within that php includes pretty much a page of html with linked css for styles. So the page source would show the title tags etc. for the layout and then later down the source it would show title tags that had been input at the content page (which obviously wouldn't work or overwrite those at the top of the page)...

 

 

 

I don't really know or understand why it's built like that but are you saying having cases for each individual page on the index.php is normal for this method?

 

Currently it is ok, there's only maybe 50-100 static content pages. I just don't get what happens if I end up with 1000s of pages - well I do know, I'll end up with a massive index.php!

No, we're saying that the standard "router" method of having a single page handle includes and template rendering is normal.  it's not normal to hand-code page titles into this central router, those should be in the templates.

 

The reason the header doesn't work in your template is probably because the header is also being set elsewhere.  You'll have to find it.

No, we're saying that the standard "router" method of having a single page handle includes and template rendering is normal.  it's not normal to hand-code page titles into this central router, those should be in the templates.

 

The reason the header doesn't work in your template is probably because the header is also being set elsewhere.  You'll have to find it.

 

Ok, worked out that if I take the <head> tags and <title> <meta> tags etc. out of layout.tpl and put it in to the template pages I can name my titles individually on the page... although doing that means that there is no title for dynamic pages as the:

 

<?php require_once("aper_consts.php"); if ( $_SERVER['HTTP_HOST'] != 'testing.local' ) echo ($title);?>

 

Code is no longer used. I guess I could write some code to say if no title is found then use the code above?

 

But then I'd still need to have:

 

case "conditions";
$content_for_layout = $_GET['action'];
break;

 

For each new page I create  :shrug:

this site isn't really designed properly, the titles should come from a controller instead of the templates, it doesn't seem like you have actual templates at all, what you have are full PHP applications simply NAMED "template." 

 

But yeah, your pages won't have a title unless you put one into the template.  Just go do that instead of trying to muck with default titles. 

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.