Basically I'm a bit stuck,
I've been following the quick start on Zend site, and wish to make a dynamic navigation to the framework, I've got layout.phtml with $this->render('navigation.phtml); this has static links, but I wish to make them pull from a database table could someone in plain english not geekcaneeze explain the correct way of doing this, IE page by page with simple step by step guide on what each page is doing, as I'm not a PHP FREAK or Zend Framework master but a web designer who wants to progress in to the world of framework development, I understand the concept the vaules of it's use.
I'm sure it will cure a lot of headaches for a lot of newbies to this. in other words after reading zend frame work referance I'm still none the wiser what they are going on about.
I've got it all working though Xampp and the file structure represent the same as
application/
config/
controllers/
layout/script/
models/
views/script/index/
views/script/error/
library/
public/
regards
Mal
Pull them out in a controller, pass them along (eg. as an array) to the view:
$this->view->yourListOfLinks = getListOfLinksFromDB();
In the view (the .phtml) example output them using foreach:
foreach($this->yourListOfLinks as $link) {
echo "$link";
}
Assuming you set up a class for your your database table (ZF - Create a Model and Database Table), you should be able to do something like this in your navigation.phtml file:
<?php
$table = new Links_Table();
$links = $table->fetchAll();
?>
<? foreach ($links as $link) { ?>
<?= $link->title ?>
<? ?>
If you're creating internal site links then you could also set up some router rewrite rules (ZF - The Standard Router).
Related
I have worked through the examples in the documentation for the Fat Free Framework, and there is one example that I cannot get to work. It is the following:
$f3=require('lib/base.php');
$f3->route('GET /',
function($f3) {
$f3->set('name','world');
$template=new Template;
echo $template->render('template.htm');
// Above lines can be written as:
// echo Template::instance()->render('template.htm');
}
);
$f3->run();
I receive an error that the Template is not found. The error points to the line in which the template.htm file is being rendered and complains of Preview->render (i.e. its superclass, instead of Template->render). I don't even see a file for a Preview class in the codebase.
Interestingly, if I use the same file for the View example (below), it works just fine.
$f3=require('lib/base.php');
$f3->route('GET /',
function($f3) {
$f3->set('name','world');
$view=new View;
echo $view->render('template.htm');
// Previous two lines can be shortened to:
// echo View::instance()->render('template.htm');
}
);
$f3->run();
However, if I am going to use this framework, I would like to be able to utilize its templating feature as well.
Does anyone with experience with this framework have any idea what could be the problem? I downloaded the code from Github (https://github.com/bcosca/fatfree).
By default F3 uses the same folder where is located your main file (where you start the framework instance). You can change this behavior by setting a new path for the UI parameter. In short:
$f3 = \Base::instance();
$f3->set('UI', path_to_your_templates);
let's say you have the following structure:
- app
-- views
--- template.htm (your template)
- public
-- index.php (where your init the framework)
-- (template files are expected here by default)
public/index.php looks like:
$f3 = \Base::instance();
$f3->set('UI', __DIR__.'/../app/views/');
$f3->route('GET /',
function($f3) {
echo Template::instance()->render('template.htm');
}
Hope it helps.
Use .html instead of .htm. Yep, it really matters.
I have no experience using fat free framework, but a general pointer on how to debug this issue.
Apparently the file not found exception is being thrown by some code inside fat free framework. Try debugging that using XDebug
I ran across this issue with version Fat Free Framework 3.5.1
The issue appears since the framework OOB (in at least this version) is wired with a sample such that the templates are looked for in the 'ui/' subfolder of the root fat free framework folder.
What tells me that? Well... the OOB config.ini has the following contents:
[globals]
DEBUG=3
UI=ui/
To easily solve the problem either:
Put the file 'template.htm' in the 'ui/' subfolder
OR
Rename the 'ui/' subfolder to whatever you like in the config.ini and put the 'template.htm' file in that location
TIP: Make sure whatever UI path you specify ends in a / and if you need to specify multiple paths you can use the | or , or ; separators (making sure each path ends in a /)
I am developing a multi-langual website. Language variables (phrases/word translations) will be entered in a specific file (one different file for each language)
I wanted to know the best way to enter the phrases/word translations, should I use a normal array?
e.g
Filename = English.php
<?php
$translations = array();
$translations['phrase1'] = "this";
$translations['phrase2'] = "that";
..
?>
and in the template file
<?php
include("English.php");
echo $translations['phrase1'];
etc...
I am pretty new to PHP so I am just looking for the best way to do it.
Any suggestions?
Thank you for your help!
There are multiple of ways to do this, the two things that pop-off my head right now are:
1) Have a look at gettext & GNU gettext page. An example implementation of this to look at is Aur Website of ArchLinux. They their app support multiple languages & its all dynamic. user can can switch between languages easily. The source code is available here, study it & see how they did it.
2) Other option could be use a framework like cakephp, as most of these frameworks have translations support
Hope it somewhat helps
What are some popular ways (and their caveats) of handling dependency path changes in scripted languages that will need to occur when deploying to a different directory structure?
Let the build tool do a textual replace? (such as an Ant replace?)
Say for example the src path in a HTML tag would need to change, maybe also a AJAX path in a javascript file and an include in a PHP file.
Update
I've pretty much sorted out what I need.
The only problem left is the URL that gets posted to via AJAX. At the moment this URL is in a JS config file amongst many other parameters. It is however the only parameter that changes between development, QA and production.
Unfortunately dynamically generated URLs as #prodigitalson suggested aren't desirable here--I have to support multiple server side technologies and it would get messy.
At the moment I'm leaning towards putting the URL parameter into its own JS file, deploying a different version of it for each of development, QA and production, additionally concatenating to the main config file when deployed.
IMO if you need to do this then youe developed in the wrong way. All of this shoul dbe managed in configuration file(s). On th ephp side you should have some kin dof helper that ouputs the path based on the config value... for example:
<?php echo image_tag('myimage.jpg', array('alt'=>'My Image')); ?>
<?php echo echo javascript_include('myscript.js'); ?>
In both these cases the function would look up the path to the javascript and image directories based on deployment.
For links you should be bale to generate a link based on the local install of the application and a set of parameters like:
<?php echo link_to('/user/profile', array('user_id' => 7)); // output a tag ?>
<?php echo url('/user/profile', array('user_id'=>7)); // jsut get the url ?>
As far as javascript goes you shouldnt have any paths hardcoded in a js file that need to be changed. You should make your ajax or things that are path dependent accept parameters and then you send these parameters from the view where you have the ability to use the same scripted configuration.. so oyu might have something like:
<script type="text/javascript">
siteNamespace.callAjax(
'<?php echo url('/user/profile/like', array('user_id' => 7)); ?>',
{'other': 'option'}
);
</script>
This way you can change all this in a central location based on any number of variables. Most MVC frameworks are going to do something like this though the code will look a bit different and the configuration options will vary.
I would take a look at Zend Framework MVC - specifcally the Config, Router, and View Helpers, or Symfony 1.4 and its Config, Routing and Asset and Url Helpers for example implementations.
I'm trying to find the best way of structuring a multi-language site navigation,.
I'm aware of the language class in CI but it seems to be more for defining random words and lines of text that are used commonly throughout the site. It appears that creating lang files for each language and then defining translations of all the links seems like the standard approach?
In past on non-codeigniter projects I’ve setup one class like this
class Link{
var $name = array();
var $url;
var $links = array();
function add_link($links){
$this->links[] = $links;
}
}
$all_sections = array();
$section = new Link();
$section->name['en'] = "Home";
$section->name['fr']] = "Uberdurky";
$section->url = "/";
$sub_section = new Link();
$sub_section->name['en'] = "About Acme Ltd";
$sub_section->name['fr'] = "Fabuka Acme Ltd";
$sub_section->url = "/about/";
$section->add_link($sub_section);
Then I have a function to loop through and output the nav, which just looks at the current name[Lang] as defined by session or URL
This to me seems simpler and less overhead - with the benefit that both the nav structure and translations are defined in one place. But I’m new to CI so I might be misunderstanding the standard approach… ? I've googled quite a bit and haven't seen a solution here in detail.
The important thing is that it works for you. There are a lot of benefits to using separate language files:
Clean separation
Only load what you need
Easy to keep track of which languages are available
Ability to let others easily translate the files
I don't see anything wrong with the way you're doing it, but if you want to optimize - don't bother defining all the different language lines. You don't need the French version defined if the language is English. Use only the ones you need, you shouldn't have to pass the whole array to add_link(), the Link class should be detecting the language and loading the appropriate array only...
...it's starting to sound like a language file might be a good idea actually.
For now you just have French and English. I'm assuming you know both languages and (Uberdurky?) are the only one working on this aspect, so it's easier for you to define them "inline". What happens when you want to support 3, 4, or 10 languages? Things will quickly become disorganized and cluttered.
However, you don't have to use the Codeigniter Language class, you might be better off using your own system for something like navigation, which tends to be littered with 1 or two word translations, and changes somewhat frequently (either per site or between sites).
Once again, it's your call. Do what works best for you now and optimize later.
This might be helpful to anyone coming across this question.
https://github.com/cflynn07/CodeIgniterInternationalizationUtility
It's a script to take a cleanly structed HTML table of language translations, and convert them into the required language files used in codeigniter.
I am building a English/french website and was wondering if there is a best practice for such a job.
Duplicating the site and making a french and english folder with the appropriate site inside.
Using PHP to swap the content with html tags.
eg. if($lang=='en'):
Use php to swap only the content leaving the html tags the same for both. eg. if statements all over the place. Would this be bad for efficiency?
Any other suggestions would be appreciated
We have a framework in place for when (if) our site goes international that works like this...
Folder structure;
/
lang/
english/
images/
text/
dutch/
images/
text/
Any text or images that are language specific are removed from the page directly and replaced by constants. eg On the login screen, we drop in;
echo TEXT_LOGIN_WELCOME;
which is defined in /lang/english/text/login.php as;
define('TEXT_LOGIN_WELCOME', 'Welcome, please login:');
but in /lang/dutch/text/login.php it's defined as;
define('TEXT_LOGIN_WELCOME', 'Welcome, please login (in dutch):');
;-)
Each language define file is named exactly the same as the page it is used for, so when we load a public-facing page, we only need to figure out which language the user speaks and we can include the relevant language define file.
The good thing about this system is that all the language info is centralised. When you need to add a new language, simply copy the main (english?) folder, rename it, zip the whole thing up and send it to a translation service to work their magic. Of course, the downside of this system is the maintenance as both languages and content grow... If anyone has any bright ideas with regard to this then I'd love to hear them!
Btw, if you end up needing to guess a user's location by IP, you might want to check out geoIP.
Use a templating system. Smarty Template Engine is probably one of the most well-known PHP ones. Not only does a templating system serve the exact purpose you're trying to accomplish, it also makes maintaining pages far easier by separating the display code from the content (which also allows you to use the same template for lots of different content pages of a similar nature).
As the simplest way I recommend you to use i18n internationalization method & gettext catalogs (.po files).
The famous WordPress project is using it as well.
1 - Duplicating the entire site will force you to repeat every code touch-up into the 2 folders :-[
2 - If you mean somenting like
<?php if($lang=='en') { ?>
<p>English text</p>
<? } else { ?>
<p>Text français</p>
<? } ?>
This solution is perfect to manage two languages in the same page.
But you still have duplicated tags.
3 - Change only content it's really satisfaction.
Maybe proliferate of if statements can weigh down php compiling... I don't know.
Anyway document can be more concise with this approach:
<?php
function interpreter($buffer) {
$pieces = explode('#', $buffer);
if (isset($_GET['lang'])) $begin=$_GET['lang'];
else $begin = 1; // 1 to display français, 2 to display english
$tot = count($pieces);
for ($i=$begin; $i<$tot; $i+=3) { // remove one language
unset($pieces[$i]); }
foreach ($pieces as $value) { // recompose the text
$output .= $value; }
return $output;
}
ob_start("interpreter");
?>
#Français#English#
<p>#English text#Texte français#.</p>
<?php ob_end_flush() ?>
The text between ob_start and ob_end_flush is parsed AFTER php compiling.
That means are affected strings coming eg. from echo statement, not inside < ?php ?> tags.
Also content coming from php include IS affected.
But NOT external css or javascript.
Keep attention delimiter # isn't a caracter yet used elsewhere.
Maybe you'll prefer to replace with || or ^^
Of course in the future you can adapt this solution into 3 languages or more. But if you have to insert the "Third language translation#" in many lines of a big site, maybe the solution from MatW fits you.