Category: Agavi

rss

Remote logging with rsyslog, php and agavi

When I tried to get rsyslog running with php I didn't face much problems. Anyways I want to share some things I noticed while getting it to work.

First of all ubuntu uses rsyslog already. Just a simple

1
# apt-get install rsyslog-mysql

and all syslog messages went directly into the database "syslog". Configure /etc/rsyslog.d/mysql.conf to insert this information into a remote machine.

Now let's get to php. You have the option to set:

1
error_log=syslog

This however does log all messages with

1
php: whatever the issue or error is

If you want to replace "php" with a custom message, you have to call

1
openlog('myapp', LOG_PID | LOG_ODELAY, LOG_USER);

before any error happens. Because LOG_ODELAY is given, it won't connect to syslog unless any error happens, so one should be save to put this call at the top of your dispatcher file.

The final log message will look like that (254 is the process id):

1
Sep 26 13:39:44 root-server myapp[254]: the issue or error

Finally I wanted to log all messages created by agavi's logging system. Since I didn't found an existing AgaviSyslogLoggerAppender, I wrote one and put it up on a github-gist.

Have fun logging!

In MyQuests, php, Agavi & Ubuntu By DracoBlue @ 13:55 26.09.2011

PhpDebugToolbar 1.3.0 (for agavi) released

Today I am pleased to announce the new version 1.3.0 of the Agavi PHP DebugToolbar for download.

New features include full propel support (thanks tim), rows+time+memory display for the database queries and some small bugfixes.

The source can be found on github now http://github.com/DracoBlue/PhpDebugToolbar, feel free to fork it and send pull request with new features!

Download for PhpDebugToolbar 1.3.0 (just 23KB) is also available here at dracoblue.net.

Short installation and configuration information can be found in the readme. Thanks to all contributors!

Configure an Agavi Site with NGINX (using PHP-FPM)

de en

I was used to configure my Agavi site with Lighttpd and recently switched to Nginx. The setup for Nginx was not so common to me, so I decided to write down what I had to do to configure it properly. Here is an example for dracoblue.net. Please change the parts of the script to suit your needs (the necessary parts are highlighted with "HINT:").

First of all install php5-fpm. On ubuntu I did it this way:

1
$ sudo apt-get install php5-fpm

Now create a new file:

1
$ vim /etc/nginx/sites-enabled/dracoblue.net

And add the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
server {
listen dracoblue.net:80;
# HINT: Add the servername, so Agavi is able to see that it's dracoblue.net
# Otherwise you'll get something like "localhost" here
server_name dracoblue.net;

location / {
# HINT: The directory where index.php is
root /home/dracobluenet/tags/1.0.0/pub;
index index.php;

# HINT: All files except those in "static" should be served by index.php
location ~* ^/(favicon.ico|robots.txt|static) {
break;
}

# HINT: All files except those in "static" should be served by index.php
if ($uri !~ "^/(favicon.ico|robots.txt|static|index.php)") {
rewrite ^/([^?]*)$ /index.php?/$1 last;
}
}

location ~ \.php($|/) {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
# HINT: The directory where index.php is + $fastcgi_script_name
fastcgi_param SCRIPT_FILENAME /home/dracobluenet/tags/1.0.0/pub$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
}

As you can see, for projects I usually have only a "static" folder which is not served by agavi. This keeps the amount of work low, which nginx needs to do for each request to decide whether it should be served by php or not. I also added robots.txt and favicon.ico since you usually have them in root, too.

Restart nginx.

1
$ /etc/init.d/nginx restart
In Articles, Linux, php, Agavi, NGINX & Lighttpd By DracoBlue @ 13:46 09.12.2010

PhpDebugToolbar 1.1.1 (for agavi) released

Today I am pleased to announce the availability of the Agavi PHP DebugToolbar for download. After the success of the Qos-Filter for agavi, I want to relaunch the useful agavi toolbar with a different name, a better look and feel and a way better code base.

Screenshot:
http://twitpic.com/show/thumb/2zgig8.png

What is PhpDebugToolbar?
Like you can see on the screenshot, it's a little Toolbar (written in JS+CSS), which keeps track of all stuff happening in your php agavi application.
So you can easily spot memory leaks, time exhausting tasks and extensive usage of database queries (with the doctrine extension).

Additionally it integrates easily into the logging system of agavi, just add the PhpDebugToolbarLoggerAppender class in your logging.xml!

Where to download Toolbar?
The source can be found on github now http://github.com/DracoBlue/PhpDebugToolbar, feel free to fork it and send pull request with new features!

Download for PhpDebugToolbar 1.1.1 (just 20KB) is also available here at dracoblue.net.

Short installation and configuration information can be found in the readme. Thanks to all contributors!

This is meant to be a lightweight alternative to the unofficial Agavi Debug Tools. It won't have FirePHP output or other neat features, which ship with Agavi Debug Tools. So if you need them: use adt, please!

In open source, php, JavaScript, Agavi & PhpDebugToolbar By DracoBlue @ 12:33 21.10.2010

Detect action/view/module and matched route in agavi

de en

For each executed Module/Action(+View) there is an ExecutionContainer (AgaviExecutionContainer). So if you want to retrieve the current executed action, module or view, best is to do

1
2
3
$action = $this->getContext()->getActionName();
$view = $this->getContext()->getViewName();
$module = $container->getModuleName();

But what if you want to retrieve the "real" action, the one and only for the request? That's a little bit more difficult, so here a short introduction how to deal with that.

Whenever the routing of agavi does the work for you, by turning the input data and webbrowser call of a user, into a action and validated data, it triggers an action, executes the view for the outputtype and fills the template. But Slots can be used from the view and trigger then again validation+action+view for their parameters.

This is why the real request route can not be found in the execution container and must be found somewhere else. In the Request.

1
2
3
$route_names = $this->getContext()->getRequest()->getAttribute(
'matched_routes', 'org.agavi.routing'
);

The route_names is nothing but an array, for instance:

1
2
3
4
5
array(
'product.edit'
// because our current route is named "product.edit"
// in routing.xml
)

This information does help us indirectly to determine the action, which got called! Since the WebRouting has also a getRoute-method, which expects just a route_name, we can retrieve the info now easily:

1
2
3
4
5
6
7
// we want to have just the first one,
// thatswhy list($first) = $array style
list($route_name) = $this->getContext()->getRequest()->getAttribute(
'matched_routes', 'org.agavi.routing'
);

$route = $this->getContext()->getRouting()->getRoute($route_name);

The new $route-Variable now holds an array of all information about the route. In our case, just some are interesting.

1
2
$module = $route['opt']['module'];
$action = $route['opt']['action'];

One really should use that only in case of a default slot, which is used directly in the output_types.xml or used on serveral different pages and has to determine on its own and by using module+action where it's actually embedded.

In open source, php & Agavi By DracoBlue @ 22:26 05.03.2009

PDT auto completion tips (for agavi)

de en

In PDT (php development tools for eclipse) you are able to do auto completion (e.g. ctrl+space) on most of the classes, as long as they are defined properly or you have a proper php-doc set up. Check that tutorial, if you want to set up PDT.

Graste posted a snippet, which tells eclipse for the project (in this example it's agavi) that the variables (which are common for templates), like $template (or $ro, $rd and so on) are defined with correct type.

Here is a slightly modified autocomplete.php:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
exit();
$slots = array();
$template = array(); // agavi 0.11 default
$t = array(); // agavi 1.0 default
$tm = new AgaviTranslationManager();
$ro = new AgaviWebRouting();
$rq = new AgaviWebRequest();
$ct = new AgaviController();
$us = new AgaviSecurityUser();
$rd = new AgaviWebRequestDataHolder();
?>

Just save that file to autocomplete.php in your project structure -> eclipse will use this classes as default, so you have neat auto completion even in agavi templates.

If you have return type for a function, and want the caller to be able to autocomplete: just use proper php-docs. For example, the following will give SomeClass-typed elements on a function test123:

1
2
3
4
5
6
/**
* @return SomeClass
*/
function test123() {
/* ... */
}

If you just want autocomplete for a specific scope, you may use the following:

1
if (false) $instance = new SomeClass();

or better

1
/* @var $instance SomeClass */

and auto completion on $instance will work like if it was a SomeClass.

Got another tip? Feel free to add it in the comments!

In open source, php, Agavi, Eclipse & PDT for Eclipse By DracoBlue @ 18:54 23.02.2009