CakePHP: force lowercase action names? - cakephp

I'm using CakePHP 2.1.0 and I noticed that if you want to access a controller's action then it doesn't matter what casing you use for the action name; you will always hit that action in the controller. However, the action will expect the view file to be named with the same casing that you used to access the action. So if I were to go to http://example.com/users/aDd, I would hit the code in the "Users" controller's "add" action as expected, but it would look for aDd.ctp, which doesn't exist. Is there a way to make it so that action names can only be accessed if they're lowercase or else they're considered a bad URL?
UPDATE: I think it's safest to do this on the CakePHP level rather than on the web server level. This way, if, for whatever reason, you want to have http://example.com/FoO be valid regardless of the casing but you want http://example.com/bar and http://example.com/users/add to only be accessible if lowercase, you can do that.
Does anybody see a flaw in adding this to the "App" controller's "beforeFilter" method?:
if ($this->request->params['controller'] != strtolower($this->request->params['controller']) || $this->action != strtolower($this->action)) {
$redirect = Router::url(array(
'controller' => strtolower($this->request->params['controller']),
'action' => strtolower($this->action)
) + $this->request->params['pass']);
foreach ($this->request->params['named'] as $key => $value) {
$redirect .= "/$key:$value";
}
$this->redirect($redirect);
}

Well. How about these lines in your AppController:
public function beforeFilter() {
$this->view = strtolower($this->view);
}

Related

How to strip HTML from a field in drupal views

I am trying to add a function to that strips the html from a field in drupal views. I found a function for sql server called "udf_StripHTML" that does that.
http://blog.sqlauthority.com/2007/06/16/sql-server-udf-user-defined-function-to-strip-html-parse-html-no-regular-expression/
I am using the following code:
/**
* Implements hook_views_query_alter().
*/
function cviews_views_query_alter(&$view, &$query) {
// Add a strip html tags from content.
$fields = array('field_data_body.body_value');
foreach ($query->where as $key1 => $value) {
foreach ($value['conditions'] as $key2 => $coditions) {
if (in_array($coditions['field'], $fields)) {
$query->where[$key1]['conditions'][$key2]['field'] = 'dbo.udf_StripHTML(' . $coditions['field'] . ')';
}
}
}
}
When views module converts the query object to a string the field become
from:
'dbo.udf_StripHTML(field_data_body.body_value)';
to:
[dbo].[udf_StripHTMLfield_data_body.body_value]
My question is, how can I add a function there?
Thank you,
You are going way too deep here friend. I'm going to assume that you're using Drupal 7, but for Drupal 8 this should be similar (since views is in core for both).
A few things about your approach:
That function is a user defined function which means that it needs to be defined at a much lower-level (in the SQL database) before you can use it in your query.
This is a red-herring approach, however, because you don't need to even touch the SQL to accomplish what you want (you can do this with PHP with strip_tags!)
You don't need a query alter hook here (we don't need to go to the database to do this). You could do this with one of the preprocess or field hooks from the field API or the views API using the function linked in my previous point.
Even better, you don't even have to touch the code to accomplish this. You can do it right in the Drupal UI.
Under the field settings for the view, select rewrite results and then Strip HTML tags. Presto, no more HTML tags in that field.
Image source: https://www.drupal.org/node/750172
Here is the solution that worked for me:
// Traverse through the 'where' part of the query.
foreach ($query->where as &$condition_group) {
foreach ($condition_group['conditions'] as &$condition) {
if (in_array($condition['field'], $fields)) {
$value = $condition['value'];
$field = $condition['field'];
$condition = array(
'value' => array(),
'field' => t('dbo.udf_StripHTML(!field) like \'#value\'', array(
'!field' => $field,
'#value' => $value)),
'operator' => 'formula',);
}
}
}

CakePHP User Lookup based on their ID

I thought this would be a relatively common thing to do, but I can't find examples anywhere, and the Cookbook's section on find() was not clear in the slightest on the subject. Maybe it's just something that's so simple Cake assumes you can just do it on your own.
All I'm looking to do here is retrieve a User's name (not the currently logged-in user…a different one) in Cake based on their ID passed to my by an array in the view.
Here's what I've got in the controller:
public function user_lookup($userID){
$this->User->flatten = false;
$this->User->recursive = 1;
$user = $this->User->find('first', array('conditions' => $userID));
//what now?
}
At this point, I don't even know if I'm on the right track…I assume this will return an array with the User's data, but how do I handle those results? How do I know what the array's gonna look like? Do I just return($cakeArray['first'].' '.$cakeArray['last'])? I dunno…
Help?
You need to use set to take the returned data, and make it accessible as a variable in your views. set is the main way you send data from your controller to your view.
public function user_lookup($userID){
$this->User->flatten = false;
$this->User->recursive = 1;
// added - minor improvement
if(!$this->User->exists($userID)) {
$this->redirect(array('action'=>'some_place'));
// the requested user doesn't exist; redirect or throw a 404 etc.
}
// we use $this->set() to store the data returned.
// It will be accessible in your view in a variable called `user`
// (or what ever you pass as the first parameter)
$this->set('user', $this->User->find('first', array('conditions' => $userID)));
}
// user_lookup.ctp - output the `user`
<?php echo $user['User']['username']; // eg ?>
<?php debug($user); // see what's acutally been returned ?>
more in the manual (this is fundamental cake stuff so might be worth having a good read)

Clearing all prefixes

I have several prefixes in play in an existing CakePHP app. I also have a bit of primary navigation in the layout that points to shared methods. I know I can explicitly set each prefix to false to avoid linking with the prefix, but is there a shortcut path that simply tells Cake to no use any prefixes no matter which one's context may currently exist?
For example, I'm on a page where a realtor can register (/realtor/users/register). I have a similar prefix for inspectors and contractors because the registration process is slightly different. Since I'm not authenticated, there's a Login link in the primary nav, but the login action is shared by all user types and should be accessed without any prefix.
<?php echo $this->Html->link( 'Login', array( 'controller' => 'users', 'action' => 'login', 'realtor' => false, 'inspector' => false, 'contractor' => false ) ) ?>
I'd like to be able to, in the link, just turn off all prefixing rather than turning off each possible prefix independently. Possible?
I know it's been 2 years since the question above was answered, though I think I found an even less intrusive way to accomplish what you want.
Set the prefix name dynamically by taking the current prefix value from $this->params and set it to false, like so
$this->Html->link('hello', array($this->params['prefix']=>false, 'controller'=>'posts','action'=>'index'));
The value of $this->params['prefix'] will be the one and relevant at that moment to set to false.
cheers
If loosing the routing capabilities is not a problem for you, you could pass a string instead of an array to the link() method:
<?php
echo $this->Html->link('Login', '/users/login');
?>
EDIT
To keep routing mechanism, here is a little Helper that would do the trick:
class MyHtmlHelper extends HtmlHelper
{
public function link($title, $url = null, $options = array(), $confirmMessage = false)
{
$prefixes = Router::prefixes();
foreach($prefixes as $prefix)
{
$url[$prefix] = false;
}
return parent::link($title, $url, $options, $confirmMessage);
}
}
Off course you could change the method name if you want to keep the standard link() method. I tested this with Cake2, but this should work with Cake1.3

CakePHP strange behavior with beforeFilter: I cannot set the variables to the view

Okay, this will require some setup:
I'm working on a method of using nice post title "slugs" in the URL's of my cakePHP powered blog.
For example: /blog/post-title-here instead of /blog/view_post/123.
Since I'm obviously not going to write a new method for every post, I'm trying to be slick and use CakePHP callbacks to emulate the behavior of PHP 5's __call() magic method. For those who do not know, CakePHP's dispatcher checks to see if a method exists and throws a cakePHP error before __call() can be invoked in the controller.
What I've done so far:
In the interest of full disclosure ('cause I have no Idea why I'm having a problem) I've got two routes:
Router::connect('/blog/:action/*', array('controller' => 'blog_posts'));
Router::connect('/blog/*', array('controller' => 'blog_posts'));
These set up an alias for the BlogPostsController so that my url doesn't look like /blog_posts/action
Then in the BlogPostsController:
public function beforeFilter() {
parent::beforeFilter();
if (!in_array($this->params['action'], $this->methods)) {
$this->setAction('single_post', $this->params['action']);
}
}
public function single_post($slug = NULL) {
$post = $this->BlogPost->get_post_by_slug($slug);
$this->set('post', $post);
//$this->render('single_post');
}
The beforeFilter catches actions that do not exist and passes them to my single_post method. single_post grabs the data from the model, and sets a variable $post for the view.
There's also an index method that displays the 10 most recent posts.
Here's the confounding part:
You'll notice that there is a $this->render method that is commented-out above.
When I do not call $this->render('single_post'), the view renders once, but the $post variable is not set.
When I do call $this->render('single_post'), The view renders with the $post variable set, and then renders again with it not set. So in effect I get two full layouts, one after the other, in the same document. One with the content, and one without.
I've tried using a method named single_post and a method named __single_post and both have the same problem. I would prefer the end result to be a method named __single_post so that it cannot be accessed directly with the url /blog/single_post.
Also
I've not yet coded error handling for when the post does not exist (so that when people type random things in the url they don't get the single_post view). I plan on doing that after I figure out this problem.
This doesn't explicitly answer your question, but I'd just forego the whole complexity by solving the problem using only routes:
// Whitelist other public actions in BlogPostsController first,
// so they're not caught by the catch-all slug rule.
// This whitelists BlogPostsController::other() and ::actions(), so
// the URLs /blog/other/foo and /blog/actions/bar still work.
Router::connect('/blog/:action/*',
array('controller' => 'blog_posts'),
array('action' => 'other|actions'));
// Connect all URLs not matching the above, like /blog/my-frist-post,
// to BlogPostsController::single_post($slug). Optionally use RegEx to
// filter slug format.
Router::connect('/blog/:slug',
array('controller' => 'blog_posts', 'action' => 'single_post'),
array('pass' => array('slug') /*, 'slug' => 'regex for slug' */));
Note that the above routes depend on a bug fix only recently, as of the time of this writing, incorporated into Cake (see http://cakephp.lighthouseapp.com/projects/42648/tickets/1197-routing-error-when-using-regex-on-action). See the edit history of this post for a more compatible solution.
As for the single_post method being accessible directly: I won't. Since the /blog/:slug route catches all URLs that start with /blog/, it'll catch /blog/single_post and invoke BlogPostsController::single_post('single_post'). You will then try to find a post with the slug "single_post", which probably won't exist. In that case, you can throw a 404 error:
function single_post($slug) {
$post = $this->BlogPost->get_post_by_slug($slug);
if (!$post) {
$this->cakeError('error404');
}
// business as usual here
}
Error handling: done.

how to get my application directory name

my url is http://localhost/myAppliation/....
I want to get the "myApplication". Which is the key word to get the value?
I think you want:
$this->webroot
or
$this->base
or
$this->here
in your controller. See http://api.cakephp.org/class/controller for definition of this variables.
Edit: Didn't see that you were using CakePHP. If you weren't using any frameworks, you would get the path via this:
First you need to reconstruct the URL.
$pageURL = (#$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
Then parse it for the path.
$parts = parse_url($pageURL);
$path = $parts['path'];
After that if you want to only get the first path, explode it on '/', strip out the null values, and then the first one will be the first path, and so on.
$paths = explode('/', $path);
foreach($paths as $key => $value)
{
if(!empty($value))
{
$new_paths[] = $value;
}
}
$first_path = $new_paths[0];
There is something called the $Name attribute, but I'm not sure if that gives you the application name. The CakePHP manual says:
PHP4 users should start out their
controller definitions using the $name
attribute. The $name attribute should
be set to the name of the controller.
Usually this is just the plural form
of the primary model the controller
uses. This takes care of some PHP4
classname oddities and helps CakePHP
resolve naming.
http://book.cakephp.org/view/52/name
http://book.cakephp.org/view/317/Cake-s-Global-Constants-And-Functions
I think you want to use the WEBROOT_DIR constant.

Resources