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.
Related
I have views that are called from different views. The customers edit view can be called from the customers list, the customers search, from within an order and so on. Some of this views are simple views other contains forms to add, edit or delete data. After the user has done what he had to to on that form he should be redirected to the calling form or to another form.
Using $this->referer() wouldn't work as some navigations have to go like this:
list order --> edit order --> delete order --> list order.
I would be fine with defining the redirection for every call so I've tried to use query strings and add ?redirectTarget=<wherever> to every link or redirection. For that I've made a controller function
in AppController.php
public function getRedirectTarget() {
if ($this->request->query('redirectTarget')) {
return $this->request->query('redirectTarget');
} else {
return array(
'controller' => 'pages',
'action' => 'home'
);
}
}
This works for forms as I can use getRedirectTarget() in my controllers but I cannot access that function from within a view to build a link. (At leas I sholdn't do that Can you call a controller function from a view in CakePHP? ) In the example from the top I have to pass the information from the order list to the edit view to build a link and to the underlying controller for the form action.
Now I have different aproaches in my mind but with none of them I'm realy happy. I'm not shure which way to go.
Is there something in CakePhp I haven't found yet?
using $this->requestAction?
changing everything to forms and buttons and doing all redirections in the controllers?
Is there a way to create a variable for every view?
As this seems to me like a comon requirement and I'm pretty new to CakePHP I'm asking for your advice.
Using requestAction should be the best way.
In the views, you can have something like
$redirect = $this->requestAction('/mycontroller/requestAction');
$url = $this->Html->link('Continue', $redirect));
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);
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'm running Cake 2 and the site is setup like:
/internal/company1
/internal/company2
/internal/company3
All of the above use the pages controller and I use routing to add pages after those urls.
e.g.
Router::connect('/company1/tests', array('controller' => 'tests', 'action' => 'index'));
I'm not using themes. The problem I've run into is say for example I want a totally different CSS layout (theme I guess?) for company1/minisite (header and all)
What's the best way to achieve this? Themes?
According to the argument passed into your controller's method, you can change your layout using the following syntax:
$this->layout = 'layout1';
Create your different layouts with different css. Hope it will work for you.
Im new to cakePHP, and Im wondering how a 'live' site does this.
I see 2 possibilities :
1) There is one controller with a bunch of pages (functions) in its (extended) AppController.
2) There are many controllers, each with a small number of pages (functions) in its (extended) AppController.
(You probably get my question already, but Im going to say it in another way too)
Should I put my contact page in a separate controller than my blog page? (I have a hunch the answer is yes.) Why?
You don't need to create a controller for everything. In fact, you shouldn't, because there are better ways around it. The more static pages you have, the more out of hand it can get.
For Static Pages
Copy pages_controller.php from the cake/libs/controller folder over to your app/controllers folder. Add the following piece of code to your display() action:
function display() {
...
$page = Inflector::slug($page);
if (method_exists($this, $page)) {
$this->$page();
}
$this->render(join('/', $path));
return;
}
Then, modify your routes.php file to add the various static pages:
Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));
Router::connect('/contact', array('controller' => 'pages', 'action' => 'display', 'contact'));
Now, the contact form is a static page, but has some logic attached to it. So, you can head over to your PagesController and create an action specifically for this (or any other page that isn't merely static):
function contact() {
if (!empty($this->data)) {
...
}
}
Basically, the route directs your static page request to the PagesController's display() action. The display action checks if a method of the same name exists. If it does, it executes that action and displays the pages/{page}.ctp view.
For Non-Static Pages, eg. Blog
Now, this definitely needs a model. In fact, multiple models (Post hasMany Comment, Post HABTM Tag). In order to manipulate and access these different models, it's better that you place the code into a separate controller.
A lot of people like to name their controllers based on their URLs. For example, they name their controller as BlogController if they want a URL such as /blog.
A method that I prefer is using routing to get the URLs that I want, and keeping controllers named as per CakePHP conventions.
Eg. A PostsController would control the Post model and related models. But if I wanted the /blog URL to display a list of all the posts, I would write a route to point it to /posts/index.
Router::connect('/blog', array('controller' => 'posts', 'action' => 'index'));
You can have additional routes too. Example: /blog/2010/06/10/whats-in-a-post to point to /posts/view/.
Again, this is just an example of what's possible. In the end, you should stick to the methods that you think helps keep your code organized for both you and your team.