Posts Tagged ‘route’
Tip: use CakePHP Route::url() for your URLs
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/html4strict.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/html.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/html4strict.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/html.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/html4strict.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/html.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/html4strict.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/html.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Routing is one of the amazing features of CakePHP because it makes the urls beautiful. We use this most of the time and last time, I blogged about the helper to shortcut ones typing when using the HTML helper. Overtime, I realize how helpful it can be. Read on.
Always use array when passing parameters when creating a link or using Router::url(), it will save you time in the future. Why? Consider this scenario:
You created a controller /users/register and in your code and you added this
<?php echo $html->link('User Registration', '/users/register')?>
Then your group or your boss decided to use a better name /register. Your initial thought would be to replace all those texts to those new ones, isn’t it? There is a better approach. Try the ffg:
In /config/router.php
<?php Router::connect('/register', array('controller'=>'users','action'=>'register')); ?>
In your view:
<?php echo $html->link('Registration', array('controller'=>'users','action'=>'register'))?>
the HTML helper uses Router::url() in its link function and the Router class magically converts array parameters to what you stated in your config. In writing urls, we have to be consistent because it can be bad for SEO when you have too many urls pointing to the same location.
Although the above works beautifully but it can be a tedious job. I wrote not long ago about the html helper that could make this approach easier so you could write the ffg in your view.
<?php echo $html->link('Registration', url( 'users', 'register', null, 'teacher' ))?>
It saves a few characters!
From the last post, I refactored it and placed it in the bootstrap so I can call it anywhere (got that idea from the google groups).
Here it is:
function url() { $args = func_get_args(); $count = func_num_args();if ( $count == 1 ) {if ( is_array( $args[0] ) ) { $args = $args[0]; $count = count($args);} else { return $args[0]; } } $short_keys = array( 0 => 'controller', 1 => 'action', 2 => 'plugin', 'c' => 'controller', 'a' => 'action', 'p' => 'plugin', );foreach ( $short_keys as $short_name => $long_name ) {if ( isset( $args[$short_name] ) ) { $args[$long_name] = $args[$short_name]; unset( $args[$short_name] ); } } // Need to explicitly assign plugin key if ( !isset( $args['plugin'] ) ) $args['plugin'] = null; if ( !isset( $args['action'] ) ) $args['action'] = 'index'; if ( !isset( $args['admin'] ) ) $args['admin'] = false;$routing = Configure::read('Routing.admin');if ( preg_match( "/^{$routing}_/", $args['action'] ) && $routing ) { $args['admin'] = true; $args['action'] = substr( $args['action'], strlen($routing) + 1 ); } return $args; }
The function above could take an array OR any number of string parameters.
url ( 'controller', 'action', 'plugin_name', ...[more params here] ) url ( array( 'c' => 'controller', 'a' => 'action', 'p' => 'plugin' ) )
The first one is strict for the first 3 parameters. The latter can be written in any order as you like. I also added support for prefix routing such as admin_*. So you can write:
url ( 'controller', 'admin_action', 'plugin_name', ...[more params here] ) url ( array( 'c' => 'controller', 'a' => 'admin_action', 'p' => 'plugin' ) )
Router::parse() can also be used instead of url() function but the latter is faster.
That’s it. Happy Baking!
Convenient $html->link()
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
Warning: fopen(/home/promet/public_html/cakephp/wp-content/plugins/devformatter/geshi/geshi/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103
I made this quick helper that extends Html Helper. While creating my very first plugin for cakephp 1.2, I decided to use Route so that I can save some space and time typing long controller names. Here is the sample code in my views:
echo $html->link( 'Link Test', array( 'controller' => 'long_controller_name', 'actions' => 'index', 'plugin' => 'long_plugin_name' ) );
I am a lazy typist and so I wanted to be able to use shortcut names for the array keys such as the following:
c = controller a = action p = plugin OR use numbers 0 = controller 1 = action 2 = plugin
I couldn’t find in the bakery any solution so I decided to create my own Html helper. Here is how you can install it:
First, add this file to your helpers folder located in
/cakedir/app/views/helpers/
Download XHTML Helper
Then, in your controller file, include the Xhtml to your helpers list. Example
class MyController extends AppController { ... var $helpers = array( 'Xhtml' ); ... }
That’s it. You can use it by doing the following:
echo $xhtml->link( 'Link Test', array( 'c' => 'long_controller_name', 'a' => 'index', 'p' => 'long_plugin_name' ) );
OR
echo $xhtml->link( 'Link Test', array( 'long_controller_name', 'index', 'long_plugin_name' ) );
Take note of using xhtml instead of html. I hope I don’t mislead anyone for using the name XHTML
. I just want an extended HTML helper.
Tags: html helper, route

