Template not found error - Fat Free Framework - php

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 /)

Related

Routing in PHP - Fat free framework gets strange results

For some reason, when I run this code, when adding an ID (the second block), my template doesn't seem to find my CSS/image files anymore.
But the code is exactly the same (apart from using an #id to request the parameter). Code framework that's been used is Fat-free framework.
This is my code:
$f3->route('GET /detail',
function($f3, $params) {
$test = new renderEngine;
$test->detail($f3, $params['id']);
}
);
$f3->route('GET /detail/#id',
function($f3, $params) {
$test = new renderEngine;
$test->detail($f3, $params['id']);
}
);
When checking in Chrome developer (or however you call the window) in the Sources tab I can see that in the first case it's:
>localhost
>
>--Audiodelight (name of my rootfolder)
>
>----css (with in that map my css files)
In the second case I get
>localhost
>
>--Audiodelight/detail (Which seems to be the problem)
>
>----css (with in that map my css files)
Does anyone have an idea why it's searching in a subdirectory? The directory it's looking for doesn't even exist.. Or know how to fix it?
Check your routing syntax http://fatfreeframework.com/base#Routing
Are you getting any PHP errors from this?

fat free framework and php -S: some URLs inexplicably not working

I'm trying to run a F3 php app under php -S, php's builtin web server (for local testing).
I have the following routes:
GET /rest/1.0/team=TeamRestController->index
GET /rest/1.0/team/#id=TeamRestController->index
Accessing these fails with 404.
When I add these routes:
GET /rest/10/team=TeamRestController->index
GET /rest/10/team/#id=TeamRestController->index
access to them work. Looks like the dot is preventing something here. The 1.0 form does work under apache.
What is happening here?
Edit: Erm, this:
while (q > request->vpath) {
if (*q-- == '.') {
is_static_file = 1;
break;
}
}
I.e. if there is a dot anywhere in the URL path then php will only serve static content, not execute scripts. What were they thinking?
Looks like a bug in the built-in server: when calling /rest/1.0/team, PHP sets $_SERVER['SCRIPT_NAME'] to /rest/1.0 even though the called script is index.php...
The intention is probably to support such calls: /rest/index.php/team but in our case, this is irrelevant since:
the script name is already passed as an argument to the server
there's no such script called 1.0
You can workaround this bug by setting the correct $_SERVER['SCRIPT_NAME'] at the top of index.php:
<?php
$_SERVER['SCRIPT_NAME']=basename(__FILE__);
$f3=require('lib/base.php');
//etc...

Yii: Creating a Demo Site Without Replicating the Code Base

I need to set up a demo site for users to try a web app before signing up. The demo would be based on production code, however, it would require minor code changes: connection to a demo database, automatic creation/login of a new guest account for each user, etc.
The obvious solution is to replicate my code base as a second demo website and edit as necessary. Keeping the demo code in sync with production code is easy enough by adding a branch in subversion. I'm less than thrilled, however, at the prospect of having to do two updates on my server (production and then demo) every time I push code from development to production.
Initially I thought I might be able to replicate the website through a module. It's unclear if this is possible, however.
Is there a mechanic in Yii to execute an altered version of a website (config file and selected controllers)?
Never do before, so just an idea
solution with few files in other dir
create a separate a demo dir and map it on your demo URL
In this dir put this index.php (may be your .htaccess too)
<?php
$yii=_PRODUCTION_PATH_.'/framework/yii.php';
$config_prod=_PRODUCTION_PATH_.'/protected/config/main.php';
$config_demo=dirname(__FILE__).'/demo_main.php';
require_once($yii);
$config = CMap::mergeArray($config_prod,$config_demo);
Yii::createWebApplication($config)->run();
the demo_main.php override the classes (user, db) to manage a better demo experience:
<?php
return array(
'basePath'=>_PRODUCTION_DIR_.DIRECTORY_SEPARATOR.'..',
'components'=>array(
'user' => array(
// here you override the user class with a DEMO only user
'class'=>'DemoUser',
)
),
solution with all files of prduction site in a different dir
Here follows the index.php in root dir
<?php
$yii='../framework/yii.php';
$configMain = include dirname(__FILE__).'/protected/config/main.php';
$configProd = include dirname(__FILE__).'/protected/config/production.php';
$configDemo = include dirname(__FILE__) . '/protected/config/demo.php';
require_once($yii);
// for the demo version
// instead of the comment can be an *if* or any solution to manage 2 configs
//$config = CMap::mergeArray($configMain,$configProd);
$config = CMap::mergeArray($configMain,$configDemo);
Yii::createWebApplication($config)->run();
demo.php is analogue to "demo_main.php" overridig classes and configs for the demo version of the site.
The testdrive demo app is configured for this - after you install, note the separate index-test.php, and protected/config/test.php.
Unlike #IvanButtinoni's suggestion, you'll need to access index-test.php, instead of index.php, so you may need to modify your .htaccess if you're using clean URLs to allow access to index-test.php.
When I do this, I usually write a custom init in the base controller.php:
public function init() {
// use test layout if using test config
if (isset(Yii::app()->params['test'])) {
$this->layout='//layouts/test';
}
parent::init();
}
Obviously, I have a test parameter in my test.php . . .
The only difference in my two layouts is that one sets the background color to be a bright yellow, just so it's very clear you're on a test site.
If I have understood well (according to the comment answers to original post) then There are several ways. Here is a link that I think can help great deal. It helped me set up and may be will help you!
In Yii 2 it will be inherently supported
http://www.yiiframework.com/wiki/33/

Changing dependency paths when deploying to different directory structure than developing in

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.

How to create dynamic links from database in Zend Framework?

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).

Categories