In CakePHP, I want to create a custom URL that points from my site to another site.
Example: example.com/google would redirect to http://www.google.com
I'm a self-taught CakePHP newcomer and just can't figure out the steps. From my homework, I think I can create a route to a controller/action in config/routes.php, but I don't the right terminology to create the action in the controller.
If you want to redirect directly form controller to an external url the we can directly use
$this->redirect('http://www.google.com');
from our controller. It will redirect you to the mentioned address. This works perfectly fine.
You don't want a "redirect", you want to create a hyperlink.
Use Cake's built-in Html helper.
In your controller...
var $helpers = array( 'Html' );
In your view...
echo $this->Html->link( 'Google link!', 'http://www.google.com/' );
A "redirect" is commonly used to refer to redirecting the script on the server side. For example, after a user fills out a Contact form you may want to email yourself the details and then redirect the user to a "Success!" page with the following controller code
$this->redirect( '/contact/success' );
Using CakePHP HTML helper is your best bet.
echo $this->Html->link('Link Text Here', 'http://www.anywebsiteyouwant.com);
If it's simple enough, you could just use straight HTML.
What you need is something like:
Router::redirect('/posts/*', 'http://google.com', array('status' => 302));
This would redirect /posts/* to http://google.com with a HTTP status of 302.
See http://book.cakephp.org/2.0/en/development/routing.html
Related
How could I make the router to link to a specific controller/action if the url as a specific keyword inside it ?
aka I'm making a game, I want the user to be able to access it's bank by entering any url with the keyword "money" inside it. That way /showmemoney , /momoney , etc. would all link to the controller Banks.
Looking for the cakeway to do this.
Read the CakePHPs official documentation about routing.
Another common use for the Router is to define an “alias” for a
controller. Let’s say that instead of accessing our regular URL at
/users/some_action/5, we’d like to be able to access it by
/cooks/some_action/5. The following route easily takes care of that:
Router::connect(
'/cooks/:action/*', array('controller' => 'users')
);
My codebase is built in Cakephp.
I have an update button which processes a "notes" field. I have a working controller update/write that redirects back to the page, so the "hard" bit is done...
However: from a usability point of view, this redirects to the raw URL, and hence to the top of the page every time.
The <input> field has an id, so I simply want to link back to it using an anchor tag.
Here's what works [controller]:
$this->redirect('/review/index/'.item->getEmployeeId());
I tried to add in the following:
$this->redirect('/review/index/'.$item->getEmployeeId().'#'.$item->getEmployeeId());
However - this seems to be stripped out... The write still works, but the anchor is stripped out.
For debugging/quick gotchas: I have tested the raw URL out and it redirects to the <input>.
Is there another way to do this? I'm assuming this is some cakephp "magic" and I simply don't know how to apend an anchor. Some google searches and poking in the API don't seem to clear things up though.
Many thanks.
Following: http://book.cakephp.org/2.0/en/controllers.html#Controller::redirect
Use this:
$url = array(
'controller' => 'review',
'action' => 'index',
$item->getEmployeeId(),
'#' => $item->getEmployeeId()
);
$this->redirect($url);
I have a site that uses CakePHP 2.x. There's a backend interface where actions use the standard Cake layouts and views, but several of the actions are also exposed to front end users as "dialogs" (same functionality, just a layout that can be put in iframe).
In app/Config/router.php I have added the following:
Router::connect('/dialog/:controller', array('action' => 'index'));
Router::connect('/dialog/:controller/:action');
Router::connect('/dialog/:controller/:action/**');
This works appropriately, but the problem starts when trying to use the HTML helper's link() method. If I try to create a link like:
$this->Html->link('edit account', array('controller' => 'users', 'action' => 'edit'));
I get the following:
edit account
When the link is within a dialog, this works great, but I don't want the non-dialog pages to link to the dialog.
How can I control which of the two URLs is used in a particular page?
Is there something I can call from within AppController once I know whether the page being rendered is a dialog or not, or even something in the call to link() that would allow me to override it.
I know there's the "prefix" option which would allow for URLs like /user/dialog_edit but I would like to maintain the /dialog/users/edit format if possible. I also know I can hard code the URL vs. passing controller/action/id/etc in an array, and I don't anticipate pathing/model names changing, but I'd like to do this the idiomatic way for CakePHP, if possible.
dynamic html redirects using routers
in the beginning I had no categories and all of my pages were root
example:
http://domain/somepage
This was great, but as my content over the years grew I need to categorize my content
so I added some routes see below
//routes.php
Router::connect(
"/:category/:slug",
array('controller' => 'controllername', 'action' => 'view'),
array(
'name'=>'[-A-Z0-9]+',
'pass' => array('category','slug')
)
);
//end
this works great and accomplished what I needed to do, but there is one problem the search engines .I need to write 301's for all of my links and I have over 8K pages.
The solution cakesphp's Router::redirect
The issue I am now having is I cant figurer out how to redirect my old links. I can for example redirect all of the links to one category, but that wont cut it. I need to redirect all of my links to the new location.
I am trying to use routes.php router :: redirect
if I do this my code it redirects to the category, but not the slug
Router::redirect(
'/:slug/*',
array(
'pass' => array('category/:slug'))
result
http://domain/category/
how can I get cake to redirect to
http://domain/category/slug ?
instead of http://domain/category/
I had all of my links pointing to the root directory
http://domain/somepage
http://domain/anotherpage
http://domain/ect
I needed to add categories
such as
`
http://domain.com/phones/samsung.php
http://domain.com/books/cakephp.php
`
I didn’t want to use .htaccees file because
My hosing provide limits me to 100 redirects
and i have over 8K links i need to redirect
I am trying to use cakes router ::redirect function in the routes.php file.
the below code works only for one category it doesn’t do it dynamically like I would like it too.
I tried to create a router class that would do this for me like you suggested, but to be honest with you I am not an expert in cakephp. Its easy to learn and a great framework I just dont know how to make my own classes or components yet. I just haven’t found good documentation to do this yet.
//routes.php
$move='category/'. stripslashes_deep ($_SERVER['REQUEST_URI']); Router::redirect('/:slug/*',$move, array('status' => 302)); code
Your best bet is to use a custom route class.
Custom routing extends the CakeRoute class and will allow you to return/add/modify parameters that are passed over.
CakePHP Custom Route Classes - How to Pass Arguments?
I am going to implement paypal in cake php. I have two conditions to post the form.
Fist posts the form to paypal, if user click on paypal button. Then form shoud be posted at paypal url at : https://www.paypal.com/cgi-bin/webscr .
If user click on submit button form should be posted to it controller.
It means i want to create two forms at a single page and want to posts at different places in certain conditions.
If I am giving custom action in form, it is adding app name+controller name before the url. like: appname/controllername/http://google.com. but want only http://google.com .I want to remove appname/controllername from the url.
How do I do this? Thanks in advance.
In your form creation call, you need to specify the URL
echo $this->Form->create('Model', array('url' => 'http://google.com'));