CakePHP: full path behind link, can't remove? - cakephp

Seems simple but I can't figure it out. Every link on my website suddenly shows the path behind the link
Example:
Not a member yet? Please register(/users/register) to add your pitch.
I can't select the path with Firebug to trace where it is coming from. Anyone has an idea? Thanks
UPDATE:
The PHP code for this link is:
echo $html->link('register', array('controller' => 'Users', 'action'=>'register'));
I use the BluePrint CSS Framework

Your problem isn't the style rule. Your problem is that you are linking a print only stylesheet from blueprint as a normal (screen, projection) stylesheet.
You need to make sure your css link has 'media' = 'print' as one of it's attributes.
IE
<?php
....
echo $this->Html->css( array(
join( DS, array( 'blueprint', 'print' )),
'stylesheet',
array( 'media' => 'print' )
);
...
?>
Somewhere in the head section of your layout.

try this...
$this->Html->link(__('register', true), array('controller' => 'Users', 'action' => 'register'));
cakePHP v. 1.3

Related

Forcing language prefix on URL in CakePHP

I'd like to force my site's URL to always have a language suffix.
So, if they type www.mysite.com it should take them to www.mysite.com/en.
I have a default language, so that should be used if it's their first time to the site. If it's not, I have a Cookie being set that I can use...but - I don't know where to use it.
I thought about checking to see if there was a "language" parameter in the URL, then if not, redirecting, but - that seems overkill - is there a better way? Can I do this in routes? or bootstrap?
The most efficient way would be through your web server. You can easily check if the request is for / (the home page) and redirect to /en.
Check the docs for what ever web server you are using, they all have something like mod_rewrite or similar.
Edit
You could set up a route like /set_default_language to redirect to in case of /, this controller can access the db and do what ever it needs.
Alternatively you can make it redirect to /your/usual/language_switch with no language specified and allow the code to use the default.
What I did:
I ended up checking in the AppController's beforeFilter() whether or not $this->request->params['langauge'] was set and if not, building the URL accordingly:
//Redirect to same url, but with language parameter
if (empty($this->request->params['language']) &&
empty($this->request->params['admin'])) {
$defaultLanguageCode = Configure::read('Languages.default.code2');
$cookiedLanguage = $this->Language->activeLanguageByCode($this->Cookie->read('lang'));
$languageToRedirectTo = (!empty($cookiedLanguage['code2'])) ? cookiedLanguage['code2'] : $defaultLanguageCode;
$newURL = '/' . $languageToRedirectTo . $this->request->here;
$this->redirect($newURL);
}
Note:
The part I couldn't figure out (until getting help in IRC) was to build the URL using $this->request->here, which is just the URL as a string. Prior to that I tried building out the array using the params array, but had no luck.
My routes (in case they help anyone)
(Keep in mind, I'm a routes noob, so - although they seem to be working for me, I do NOT guarantee they're done well!)
//root URL and root URL w/ language
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); // eg: www.google.com takes them to pages/display/home
Router::connect('/:language', array('controller'=>'pages', 'action' => 'display', 'home'), array('language'=>'[a-z]{2}')); // eg: /en takes them to pages/display/home and sets language
//pages (eg. /en/r/the_matrix or /r/the_matrix)
Router::connect('/:language/r/:slug/*', array('controller'=>'pages', 'action'=>'display'), array('language'=>'[a-z]{2}', 'pass'=>array('slug')));
Router::connect('/r/:slug/*', array('controller'=>'pages', 'action'=>'display'), array('pass'=>array('slug')));
//adds language to default URL
Router::connect('/:language/:controller/:action/*', array(), array('language'=>'[a-z]{2}'));
//Route prefixes
Configure::write('Routing.prefixes', array('admin'));
//User related
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/myaccount', array('controller' => 'users', 'action' => 'my_account'));
//
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';
If you don't want to redirect the requestAction calls from within your controllers or views you should add the following condition to your IF statement
if (empty($this->request->params['language']) &&
empty($this->request->params['admin']) &&
empty($this->request->params['requested'])) {
...
}

What's the best way to 301 redirect pages in CakePHP?

I have rewritten my site in Cakephp and choosen to keep the new Cakephp structure. I was wondering if I could use routing in Cakephp for 301-routing (permanently moved).
I want to redirect resources.php, languages.php, clips.php, possibly *.php, to /resources/, /languages/, /clips.
Can this type of 301 redirecting be easily done in CakePHP? I could even write a simple admin-interface to add 301-links, e.g. from a MySQL table to easily administer redirects. Or is it better to do this manually via mod_rewrite?
I'm not sure about the best way, but I would first put routing at routes php like:
Router::connect('/resources.php', array(
'controller' => 'resources',
'action' => 'index'
)
);
(and so on)
After that check at start of the action function which route was used, and if *.php route was used do a 301 redirect:
$this->redirect(array('controller' => 'resources', 'action' => 'index'), 301);
I guess there is also "smarter" way to implement this but this was the idea. (use of before_filter etc)
Since CakePhp 2.x there is Router::redirect() method.
So you could add redirection in your routs:
Router::redirect(
'/resources.php',
array(
'controller' => 'resources',
'action' => 'index'
),
array('status' => 301)
);
The third parameter array('status'=>301) is not necessary because 301-redirect is used by default.
See Redirect routing — CakePHP Cookbook v2.x documentation.

Routes configuration for named arguments in CakePHP

In my Cake application I have a controller "completed_projects". Its index action takes no arguments and lists some projects. Different pages can be accessed by
example.com/completed_projects/index/page:23 etc.
I want to make the url's like this:
example.com/portfolio/page23
Obviously I need to make some routes for this. I've tried many of them like:
Router::connect('/portfolio/page:num', array('controller' => 'completed_projects', 'action' => 'index'), array('pass'=>'page:num', 'num'=>'[0-9]+'));
and also:
Router::connect('/portfolio/:page:num', array('controller' => 'completed_projects', 'action' => 'index'), array('named'=>'num', 'page'=>'page', 'num'=>'[0-9]+'));
I also tried modifying them again and again but none of them works well.
I am using CakePHP 1.3. Any help will be appreciated.
Router::connect('/portfolio/page:page_num',
array('controller'=>'completed_projects', 'action'=>'index'),
array('page_num'=>'[\d]+')
);
In your controller, access page_num with:
$this->params['page_num'];

CakePHP - how to use $html->link inside an element

How can I use $html->link from within an element?
Thanks,
Tee
In both 1.2 and 1.3,this should work:
echo $html->link('linkname',array('controller'=>'somecontroller','action'=>'someaction/somearguments'));
Update
The html helper has changed a little in version 2.x.An example from the cook book
echo $this->Html->link('Enter', '/pages/home', array('class' => 'button', 'target' => '_blank'));
The new version of Cakephp has easier ways to work with html->links, definitely you can convert links into buttons.
If you want using bootstrap or some css however for normal links you can use:
<?= $this->Html->link ('Sometexthere', ['controller' =>'myController', 'action' =>'myAction']); ?>

how to point to my correct address in this situation?! need help

I plan to add up a calender function to my system, but i had mess up with the link which not link to the direct address that i want..
i am still new to cakephp, need some help to get understand the code and the correct way for me to point to the correct address i want..
here is some of the code in the controller, other side is not a problem to run just left the pointing address which had make me mad..
when i press to the link, which i want to view my data, it's just jump to the other side that not in my set up.
// here is the declaration which in the original controller that i learn from the net.
$view_base_url = $this->webroot. 'calendars';
//this the original link that i using which cant point to my correct side.
$data[$day] .= 'a href="'.$view_base_url.'/view/'. $v['Calendar']['id'] . '">' . $v['Calendar']['name'] . '</a>';
in the details,
i wanted to link to my side with just the calendars/view/id of the link i point.
but with this code, i will redirect to the app/webroot/view/id side.
can i change the $this->webroot to just link to where i want??
if i not using the $this->webroot, i am not able to redirect while i click other page in the calendar..
it might be a poor explanation, cause i am still new to cakephp.
just any 1 can kindly drop me a comment what i can do?
You should use the Router class to create links to within your application. For example:
Router::url(array('controller' => 'my_controller', 'action' => 'view', $id));
// returns, for example, /my_controller/view/5
Router::url(array('controller' => 'my_controller', 'action' => 'view', $id), true);
// returns, for example, http://example.com/root_directory/my_controller/view/5
This method is used by many functions throughout Cake, for example by the HTML Helper:
echo $html->link('My Link', array('controller' => 'my_controller', 'action' => 'view', $id));
// echos:
My Link
The URLs that Router returns are based on the routes defined in app/config/routes.php. This way you can define handy shortcuts:
Router::connect('/view/*', array('controller' => 'my_controller', 'action' => 'view'));
echo $html->link('My Link', array('controller' => 'my_controller', 'action' => 'view', $id));
// echos:
My Link
This is the way you should handle all links in your Cake application. You shouldn't make links in the Controller, only in the View, using the HTML Helper. In the rare case were you do need links in the Controller, use Router::url().

Resources