View previous topic :: View next topic |
Author |
Message |
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
Posted: Wed May 31, 2006 5:46 am Post subject: How to create a site through one index.php controller file |
|
|
Here is a simple example to demonstrate an easily maintainable way of setting page loading based on the current URL. It holds all of your available pages in an associative array .. This could easily be pulled from a database.
I have also included template code to generate a simple menu for you.. You could of course just construct the URL's by hand in your templates if this does not serve your needs.
For brevity, I did not include any css or table structure for the index.tpl file.
index.php
Code: |
<?php
/**
* Website controller
* @author www.ideamesh.com
*/
//Include and instantiate Smarty
include('Smarty.class.php');
$smarty =& new Smarty();
//Setup the url var we are looking for to control page display
$page_var = 'page';
//Using the $_REQUEST scope so that the page can be passed in via $_POST or $_GET
$page_request = $_REQUEST[$page_var];
//This array holds the relationship between the page variable and the template to load.. This info could also be retrieved from a db
$menu = array (
'home' => 'home.tpl',
'about us' => 'aboutus.tpl',
'our system' => 'system.tpl',
'products' => 'products.tpl'
);
//Check if the requested page was found in the menu
if ( array_key_exists ( $page_request, $menu ) )
$template = $menu[$page_request];
//If not set the default page
else
$template = 'home.tpl';
//Assign info to Smarty and display
$smarty->compile_id = $template;
$smarty->caching = 1;
$smarty->assign('menu', $menu);
$smarty->assign('template', $template);
$smarty->assign('page_var', $page_var);
$smarty->display('index.tpl');
?>
|
index.tpl
Code: |
{include file="menu.tpl"}
{include file=$template}
|
menu.tpl
Code: |
{* We generate the menu list from the available pages in the menu *}
<ul id="navigation">
{foreach key=url_val item=template_name from=$menu}
<li>
<a href="{$SCRIPT_NAME}?{$page_var}={$url_val}">
{$url_val}<br />
</a>
</li>
{/foreach}
</ul>
|
_________________ Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template |
|
Back to top |
|
Phunky Smarty Rookie
Joined: 19 Apr 2007 Posts: 5
|
Posted: Thu Apr 19, 2007 1:10 pm Post subject: |
|
|
How would you go about using this for including dynamic pages?
Lets say we was including 'news.tpl' which would list some news items pulled out of a database.
Ideally we would want to keep the database logic in 'news.php' with 'news.tpl' just having the presentation elements.
I cant seem to pass any vars through to the included .tpl file |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Thu Apr 19, 2007 2:25 pm Post subject: |
|
|
I don't know if this is a very smart approach, but maybe you could do something like this:
You've got a news.tpl file to list your news items.
Then you've got a news.php file to get them out of the database and fill your template variables.
Now in index.php you tell index.tpl to include news.tpl and additionally you include news.php in index.php.
index.php
Code: | include ('news.php');
$smarty->assign('template', 'news.tpl');
|
Now all the variables news.php assignes should be available in news.tpl. |
|
Back to top |
|
Phunky Smarty Rookie
Joined: 19 Apr 2007 Posts: 5
|
Posted: Thu Apr 19, 2007 3:04 pm Post subject: |
|
|
That does work, but i cant help but think this is the wrong way to approach it.
I guess what i need is a wrapper or something to setup all the MySQL and other settings that are global throughout the differnt elements of the site and then just call the .php and .tpl few Smarty as you normally would |
|
Back to top |
|
Phunky Smarty Rookie
Joined: 19 Apr 2007 Posts: 5
|
Posted: Thu Apr 19, 2007 3:16 pm Post subject: |
|
|
Just had a thought, if i was to push everything through classes that was then loaded in the index.php i should then be able to gather all the data i need within any .tpl that is included in the index.tpl? |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
czarnowski Guest
|
Posted: Sat Apr 21, 2007 1:07 pm Post subject: |
|
|
I have made a test script with minimal changing:
Code: |
<?php
$timeparts = explode(" ", microtime());
$starttime = $timeparts[1].substr($timeparts[0], 1);
include('Smarty.class.php');
$smarty =& new Smarty();
$smarty->compile_check=false;
$page_var = 'page';
$page_request = $_REQUEST[$page_var];
$root_url ="http://".$_SERVER['SERVER_NAME'].'/simple';
$inhalt = array (
'Home' => 'home.tpl',
'Firma' => 'firma.tpl',
'Produkte' => 'produkte.tpl',
'Marmor' => 'marmor.tpl',
'Stein' => 'stein.tpl',
'Eisen' => 'eisen.tpl',
'Bleche' => 'bleche.tpl',
'Kontakt' => 'kontakt.tpl'
);
$menu = array (
'SECTION',
'Home',
'Firma',
'Produkte',
'SECTION',
'Marmor',
'Stein',
'Eisen',
'ENDSECTION',
'Bleche',
'Kontakt',
'ENDSECTION'
);
if ( array_key_exists ( $page_request, $inhalt ) )
$template = $inhalt[$page_request];
else
$template = 'home.tpl';
$smarty->assign('root_url', $root_url);
$smarty->assign('menu', $menu);
$smarty->assign('template', $template);
$smarty->assign('page_var', $page_var);
$smarty->display('index.tpl');
$timeparts = explode(" ", microtime());
$endtime = $timeparts[1].substr($timeparts[0], 1);
$gesamt = $endtime-$starttime;
echo "<!--Generated in ".$gesamt." seconds and ".(function_exists('memory_get_usage')?memory_get_usage(true):'n/a')." bytes of memory-->";
if (function_exists('memory_get_peak_usage'))
echo "\n"."<!--Peak memory needed ".memory_get_peak_usage(true).' Bytes.-->';
?>
|
The menu is a comnination of all and another idea from the forum.
Here the template:
Code: | {foreach from=$menu item=section_item}
{if $section_item=='SECTION'}
<ul>{capture name=closing} {/capture}
{elseif $section_item=='ENDSECTION'}
</li></ul>
{else}
{$smarty.capture.closing}
<li><a href="{$root_url}/index.php?page={$section_item}">{$section_item}</a>
{capture name=closing}</li>{/capture}
{/if}
{/foreach}
|
and here you can see a small demo
http://powercms.org/simple
and here the same demo with template lite
http://powercms.org/simplel
template lite has here only little advantages with speed and memory, so it is not realy interesting me. |
|
Back to top |
|
eth0 Smarty n00b
Joined: 03 May 2007 Posts: 3
|
Posted: Wed May 09, 2007 10:10 pm Post subject: |
|
|
My index.php
Code: | include("./inc/global.php");
if ( $ifc0nfig->smarty->template_exists( $ifc0nfig->navi->pages["currentTPL"] ) )
{
if ( file_exists( $ifc0nfig->navi->pages["currentPHP"] ) )
{
include( $ifc0nfig->navi->pages["currentPHP"] );
}
$ifc0nfig->smarty->assign( "content" , $ifc0nfig->smarty->fetch( $ifc0nfig->navi->pages["currentTPL"] ) );
}
else
{
$ifc0nfig->smarty->assign( "content" , $ifc0nfig->smarty->fetch( $ifc0nfig->navi->pages["404"] ) );
}
$ifc0nfig->smarty->display( "index.tpl" );
|
My global.php just initializes classes such as smarty, navigation, MySQL, etc. All called through the $ifc0nfig object.
My navi.php
Code: | <?php
if( !defined( "ifc0nfig" ) )
{
// naughty, naughty...
exit( "Hello :-)" );
}
class navi
{
var $currentPage = null;
var $menuArray = null;
var $pages = null;
function navi( &$ifc0nfig )
{
$getPage = isset( $_GET["p"] ) ? $_GET["p"] : "home";
$queryRes = $ifc0nfig->MySQL->doQuery( "SELECT `m_id` , `name` , `display_name` , `hide` FROM `menu`" );
while( $row = $ifc0nfig->MySQL->fetchAssoc( $queryRes ) )
{
$row["selected"] = false;
$this->menuArray[] = $row;
}
foreach ($this->menuArray as $key => $val)
{
if( $getPage != NULL && $getPage == $val["name"] )
{
$this->menuArray[$key]["selected"] = true;
$this->currentPage = $this->menuArray[$key]["name"];
}
}
//
// Setup default pages array
//
$this->pages["404"] = "page/404.tpl";
$this->pages["currentTPL"] = "page/" . $this->currentPage . ".tpl";
$this->pages["currentPHP"] = ROOT_DIR . "tpl/php/" . $this->currentPage . ".php";
//
$ifc0nfig->smarty->assign("menuArray" , $this->menuArray );
}
}
?>
|
My index.tpl just has the usual stuff and prints out $content and $menuArray like so:
Code: |
{foreach item=menu from=$menuArray}
{if $menu.hide == false}
<li{if $menu.selected === true} class="selected"{/if}><a href="?p={$menu.name}">{$menu.display_name}</a></li>
{/if}
{/foreach}
{content} |
Does the job nicely |
|
Back to top |
|
chantown Smarty Rookie
Joined: 30 Jul 2007 Posts: 5
|
Posted: Sat Aug 25, 2007 4:12 am Post subject: |
|
|
Is there any disadvantage of using this index.php "controller" method?
(I know Joomla uses this) |
|
Back to top |
|
manjcupuo Smarty n00b
Joined: 29 Oct 2007 Posts: 1
|
Posted: Mon Oct 29, 2007 4:19 am Post subject: |
|
|
chantown wrote: | Is there any disadvantage of using this index.php "controller" method?
(I know Joomla uses this) |
According to a source, it makes it very easy to deal with “global operations”, which have to happen on every page of your site, such as logging, authentication / session checking and dealing with such things as HTTP client cache headers. But such problem can be solve. _________________ All about phentermine diet pills. cheap phentermine for quick weight loss. |
|
Back to top |
|
Babysittah Smarty n00b
Joined: 28 Nov 2007 Posts: 3
|
|
Back to top |
|
bladetmc Smarty n00b
Joined: 20 Dec 2007 Posts: 3
|
Posted: Thu Dec 20, 2007 12:10 pm Post subject: |
|
|
manjcupuo wrote: | chantown wrote: | Is there any disadvantage of using this index.php "controller" method?
(I know Joomla uses this) |
According to a source, it makes it very easy to deal with “global operations”, which have to happen on every page of your site, such as logging, authentication / session checking and dealing with such things as HTTP client cache headers. But such problem can be solve. |
The only disadvantage is that it is getting complex And a small security risk.
(When calling the controller or module way dynamically, u should be aware that the users cannot call stuff outside that specific filedir). Most common used fault is bypassing those with simple domain/controller/../uploaddir/somethingnaughty.php/
But i rather dont see J00mla, not a very good example. If u use one, there are others wich are better made then that. (Drupal, CakePHP, Zend, CodeIgniter).
Or make your own based on for example MVC model, but please make it all classes instead of the example above.
index.php should only contain:
include(myframework.php);
$f00 = new MyFrameWork();
Then do debugging, logging, database access etc (smarty isnt a part of it, especially when using MVC. Smarty is needed to do the View and should be called at the end of the chain ) |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Thu Dec 20, 2007 4:17 pm Post subject: |
|
|
Try monte's new project: http://www.tinymvc.com _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
bladetmc Smarty n00b
Joined: 20 Dec 2007 Posts: 3
|
Posted: Thu Dec 20, 2007 5:06 pm Post subject: |
|
|
I already looked at it Indeed very tiny, but didnt had time to test it tbh to see if it is as quick as he says it is.
But mine is better, but aint open source. Only reason that the more ppl know the code, the unsecure it is (its called bladesdefence pattern ).
Big Disadvantage, i have todo everything myself |
|
Back to top |
|
jetmuzer Smarty n00b
Joined: 16 Dec 2007 Posts: 2 Location: UK
|
Posted: Thu Dec 20, 2007 5:45 pm Post subject: |
|
|
Thanks, that is very helpful listing _________________ music store mp3 music mp3 music download |
|
Back to top |
|
|