WURFL with Zend Framework ignoring cache directory configuration - zend-framework

I'm following the tutorial on setting up WURFL with Zend Framework to enable easy mobile browser detection.
http://framework.zend.com/manual/en/zend.http.user-agent.html#zend.http.user-agent.quick-start
I have got it setup to the point where it can detect a desktop browser and give me all the details and features of that browser, but when I try to access the website using an iPhone (mobile safari) it throws an error when trying to write to the cache directory.
Here's what I'm seeing in my error logs:
2011-06-08T22:32:34-07:00 ERR (3): The file cache directory does not exist and could not be created. Please make sure the cache directory is writeable: /var/tmp
However in my configuration at /application/configs/wurfl-config.php I have set the cache directory to the following:
<?php
$resourcesDir = dirname(__FILE__) . '/../../data/wurfl/';
$wurfl['main-file'] = $resourcesDir . 'wurfl-2.0.27.zip';
$wurfl['patches'] = array($resourcesDir . 'web_browsers_patch.xml');
$persistence['provider'] = 'file';
$persistence['dir'] = $resourcesDir . '/cache/';
$cache['provider'] = null;
$configuration['wurfl'] = $wurfl;
$configuration['persistence'] = $persistence;
$configuration['cache'] = $cache;
I've also ensured it is writable by the server, but wurfl seems to think my cache directory is still /var/tmp
How can I get wurfl to observe my cache directory setting?
Notes: The tutorial uses wurfl-1.1 as the example, I have only been able to find wurfl-1.3 on sourceforge. This may be an issue.
Notes: I have these lines in my application.ini file:
; WURFL
resources.useragent.wurflapi.wurfl_api_version = "1.1"
resources.useragent.wurflapi.wurfl_lib_dir = APPLICATION_PATH "/../library/wurfl-php-1.3.0/WURFL/"
resources.useragent.wurflapi.wurfl_config_file = APPLICATION_PATH "/configs/wurfl-config.php"

Not sure if this is the correct way to fix it, but for me the issue was solved by adding an extra .dir after the persistence.dir key (using WURFL 1.3.0):
In application.ini: (I don't use the php config file as I prefer not to mix in php code if I can use .ini directives)
resources.useragent.wurflapi.wurfl_config_array.persistence.dir.dir = APPLICATION_PATH "/../data/wurfl/cache/"
So my complete config for WURFL looks like this in Zend's application.ini:
; Mobile device detection
resources.useragent.storage.adapter = "Session"
resources.useragent.wurflapi.wurfl_api_version = "1.1"
resources.useragent.wurflapi.wurfl_lib_dir = APPLICATION_PATH "/../library/WURFL/"
resources.useragent.wurflapi.wurfl_config_array.wurfl.main-file = APPLICATION_PATH "/../data/wurfl/wurfl.xml"
resources.useragent.wurflapi.wurfl_config_array.wurfl.patches[] = APPLICATION_PATH "/../data/wurfl/web_browsers_patch.xml"
resources.useragent.wurflapi.wurfl_config_array.persistence.provider = "file"
resources.useragent.wurflapi.wurfl_config_array.persistence.dir.dir = APPLICATION_PATH "/../data/wurfl/cache/"
perhaps a bug in the framework regarding how it reads the config array it's being passed?

I just resolved the problem ;)
remove the [] from the code line below:
resources.useragent.wurflapi.wurfl_config_array.wurfl.patches[] = APPLICATION_PATH "/../data/wurfl/web_browsers_patch.xml"
transform it to:
resources.useragent.wurflapi.wurfl_config_array.wurfl.patches = APPLICATION_PATH "/../data/wurfl/web_browsers_patch.xml"

It seems the format of the parameters has changed in version 1.3 - the WURFL docs here have the details and an example file.
So for the original question, the $persistence['dir'] line needs to be changed to:
$persistence['params'] = array(
'dir' => $resourcesDir . '/cache/'
);

I solved the problem using Wurfl 1.3.1 and reading this:
http://wurfl.sourceforge.net/nphp/

With regards to Jens Wegar's answer above, there is a bug-fix request in to fix this as it's not clear.
http://framework.zend.com/issues/browse/ZF-12284

Did you configure the UserAgent resource to use the settings you are showing here?
You have to add resource.useragent.wurfl_* entries into your application.ini file.
Here is a sample:
resources.useragent.wurflapi.wurfl_api_version = "1.1"
resources.useragent.wurflapi.wurfl_lib_dir = APPLICATION_PATH "/../library/WURFL/"
resources.useragent.wurflapi.wurfl_config_file = APPLICATION_PATH "/configs/wurfl-config.php"

Related

Set APPLICATION_ENV in Zend framework 3

I've enabled development mode using composer development-enable . How can I check in my module's config/module.config.php that development is enabled or not?
I've also added SetEnv APPLICATION_ENV development in public/.htaccess and tried to use it in module.config.php using echo APPLICATION_ENV;exit; but it doesn't give me environment.
How I can set and get application environment in zend framework 3?
use the correct function ;)
$environment = getenv('APPLICATION_ENV');
Usage for config in ZF2/3:
$env = getenv('APPLICATION_ENV'); // Expect null or "development"
$modules = [];
if ($env === 'development') {
//Array of modules used only for development
$modules = array_merge_recursive($modules, [
'Zf2Whoops',
]);
}
In just Zend Framework nothing special happens with the usage of composer development-enable. However, if you use Apigility at some point, it will create a development.config.php file for you which disables application caches.

PHP Built-in Webserver and Relative Paths

TL;DR
Does PHP 5.4 built-in webserver have any bug or restriction about relative paths? Or does it need to be properly (and additionally) configured?
When I used to programming actively I had a system working under URI routing using these lines in a .htaccess file:
RewriteEngine On
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [L]
The FrontController received the Request, find the proper route from given URI in a SQLITE database and the Dispatcher call the the Action Controller.
It worked very nicely with Apache. Today, several months later I decided to run my Test Application with PHP 5.4 built-in webserver.
First thing I noticed, obviously, .htaccess don't work so I used code file instead:
<?php
if( preg_match( '/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"] ) ) {
return false;
}
include __DIR__ . '/index.php';
And started the webserver like this:
php.exe -c "php.ini" -S "localhost:8080" "path\to\testfolder\routing.php"
So far, so good. Everything my application need to bootstrap could be accomplished by modifying the include_path like this:
set_include_path(
'.' . PATH_SEPARATOR . realpath( '../common/next' )
);
Being next the core folder of all modules inside a folder for with everything common to all applications I have. And it doesn't need any further explanation for this purpose.
None of the AutoLoader techniques I've ever saw was able to autoload themselves, so the only class manually required is my Autoloader. But after running the Test Application I received an error because my AutoLoader could not be found. o.O
I always was very suspicious about realpath() so I decided to change it with the full and absolute path of this next directory and it worked. It shouldn't be needed to do as I did, but it worked.
My autoloader was loaded and successfully registered by spl_autoload_register(). For the reference, this is the autoloading function (only the Closure, of course):
function( $classname ) {
$classname = stream_resolve_include_path(
str_replace( '\\', DIRECTORY_SEPARATOR, $classname ) . '.php'
);
if( $classname !== FALSE ) {
include $classname;
}
};
However, resources located whithin index.php path, like the MVC classes, could not be found. So I did something else I also should not be doing and added the working directory to the include_path. And again, manually, without rely on realpath():
set_include_path(
'.' . PATH_SEPARATOR . 'path/to/common/next'
. PATH_SEPARATOR . 'path/to/htdocs/testfolder/'
);
And it worked again... Almost! >.<
The most of Applications I can create with this system works quite well with my Standard Router, based on SQLITE databases. And to make things even easier this Router looks for a predefined SQLITE file within the working directory.
Of course, I also provide a way to change this default entry just in case and because of this I check if this file exist and trigger an error if it doesn't.
And this is the specific error I'm seeing. The checking routine is like this:
if( ! file_exists( $this -> options -> dbPath ) ) {
throw RouterException::connectionFailure(
'Routes Database File %s doesn\'t exist in Data Directory',
array( $this -> options -> dbPath )
);
}
The dbPath entry, if not changed, uses a constant value Data/Routes.sqlite, relatively to working directory.
If, again, again, I set the absolute path manually, everything (really) works, the the Request flow reached the Action Controllers successfully.
What's going on?
This a bug in PHP's built-in web server that is still not fixed, as of PHP version 5.6.30.
In short, the web server does not redirect to www.foo.com/bar/ if www.foo./bar was requested and happens to be a directory. The client being server www.foo.com/bar, assumes it is a file (because of the missing slash at the end), so all subsequent relative links will be fetched relative to www.foo.com/instead of www.foo.com/bar/.
A bug ticket was opened back in 2013 but was mistakenly set to a status of "Not a Bug".
I'm experiencing a similar issue in 2017, so I left a comment on the bug ticket.
Edit : Just noticed that #jens-a-koch opened the ticket I linked to. I was not awar of his comment on the original question.

Setting output_style for SCSS using Compass in Sinatra app

I'm looking at the Compass-Sinatra starter file on GitHub. Is there a way to set the output_style for an scss file in Sinatra? Ideally I would like to set the style to :expanded when in development.
I think I'm having trouble understanding how sass(:"stylesheets/#{params[:name]}", Compass.sass_engine_options ) works and where I can set those options.
I found that adding the output_style setting to the compass.config file works for changing the output_style. It can either go in the if defined?(Sinatra) block or in the configuration block at the bottom of the compass.config file.
# compass-sinatra-starter/config/compass.config
if defined?(Sinatra)
# This is the configuration to use when running within sinatra
project_path = Sinatra::Application.root
environment = :development
output_style = :expanded # This is where you can set the output_style
else
# this is the configuration to use when running within the compass command line tool.
css_dir = File.join 'static', 'stylesheets'
relative_assets = true
environment = :production
end
# Or if you wanted to have the output_style set for all environments(?)
# This is common configuration
output_style = :compressed
sass_dir = File.join 'views', 'stylesheets'
images_dir = File.join 'static', 'images'
http_path = "/"
http_images_path = "/images"
http_stylesheets_path = "/stylesheets"
Note: stop/start the server if you change the settings if you don't see the change.
For example, I have a styles.scss file in views/stylesheets/styles.scss then if I go to http://localhost:4567/stylesheets/styles.css I'll get the .scss file compiled in the browser to .css. Changing the output_style, start/stop the server the .css output_style changes. I don't know if using reloader would work, but it might avoid the stop/start?
I found a couple of other good resources.
Andrew Stewart has a blog post and a GitHub template
Originally I was trying to learn about media queries in Sass(scss) with Sinatra and found a great video Ben Schwarz posted, but it doesn't go into the nitty gritty of setting up. It's more about the media query. Ben also has the source on GitHub.
But it seems like AssetPack is the best way to go for serving assets.

Setup of Doctrine CLI not quite working

Still struggling to get doctrine working properly on my system. I have a yaml file that describes the structure of my database and tables. (I am following the tutorial on zendcast). I have my doctrine script file as
#!/usr/bin/env php
<?php
chdir(dirname(__FILE__));
include('doctrine.php');
and the doctrine.php file contains
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/..'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->getBootstrap()->bootstrap('doctrine');
$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
My application.ini excerpt is
autoloaderNamespaces[] = "Doctrine_"
;--
;Database
;--
doctrine.dsn = "mysql://root:softna#localhost/gepm2"
doctrine.data_fixtures_path = APPLICATION_PATH "/configs/data/fixtures"
doctrine.sql_path = APPLICATION_PATH "/configs/data/sql"
doctrine.migrations_path = APPLICATION_PATH "/configs/migrations"
doctrine.yaml_schema_path = APPLICATION_PATH "/configs/schema.yml"
doctrine.models_path = APPLICATION_PATH "/../library/Gepm/Model"
When I run:
D:\www\gepm2\application\scripts>php doctrine build-all-reload
I get the ff feedback:
D:\www\gepm2\application\scripts>php doctrine build-all-reload
build-all-reload - Are you sure you wish to drop your databases? (y/n) y
build-all-reload - Successfully dropped database for connection named 'doctrine'
build-all-reload - Successfully created database for connection named 'doctrine'
build-all-reload - Created tables successfully
build-all-reload - Data was successfully loaded
However when I check mysql only the database is created no tables are generated. I have struggled with this for a whole day. Someone help.
EDIT
All right I got it working now.But to be honest with you the only changes I made to the code was to
1. set the time zone,
2. remove the underscore from the end of the utoloaderNamespaces[] value so utoloaderNamespaces[] = "Doctrine_" became autoloaderNamespaces[] = "Doctrine" and
3. move the doctrine database declarations to the bottom of the [production] section in the applications.ini
I am so far behind on schedule with what I am working on that I cannot mess about with these changes to see which one or why it worked. I will do say later when I have some down time. In the meantime a more knowledgeable geek may be able to explain why it may have worked. cheers.
The way doctrine creates tables in the db is by loading the models and reading the definitions within each of these models. If the CLI is not able to load the models then the table creation will fail. Unfortunately the CLI task does not report this.
Further I would advise you to check other CLI tasks such as generate-migrations-diff and migrate. I have had issues with these in the past and have resolved them successfully.

Zend Framework 1.1 Modules setup

i used zend_tool to setup a project then to create module blog with index controller etc but i guess the default config setup by zend_tool does not work with modules so i edited it
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.moduleDirectoryControllerName = "controllers"
i guess these are required for modules? also i moved the folders, controllers, models, views into the modules/ folder
but i get a blank screen when i try to go to http://servername which shld load Default module's index controller and action. even if i try to go http://servername/nonexistentpage it also shows a blank screen instead of a 404
You don't have to move controllers, models, and views.
These are directories of the default module, which is not placed in modules directory (by default).
All you need is:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =
If you want to place default module in the modules too, you have to set up the app like this:
; Default Application Resource Namespace
appnamespace = "YourPrefix"
; FrontController Resource Settings
resources.frontController.defaultController = "index"
resources.frontController.defaultAction = "index"
resources.frontController.defaultModule = "modulename"
resources.frontController.prefixDefaultModule = true
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.params.displayExceptions = 1
The reason you do not see anything is that the app throws errors, which are not shown due to your configuration. Try these settings:
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
Ensure you have SetEnv APPICATION_ENV development in your .htaccess
Upgrade Zend Framework to the newest version. Newest Zend Tool generates /docs directory with README.txt, which describes how to set up virtual host.
Hope this helps :)
And…
Welcome to the SO!