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.
Related
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.
I am very new to cakephp.
I have my project 'registration' in the workspace. I have created an IndexController, which contains method index().
When I run my project by using workspace/registration/ it displays the following error:
Error: WorkspaceController could not be found. Create the class WorkspaceController below in file: app/Controller/WorkspaceController.php.
I want to exicute IndexController first. Somebody advised me to change the default route. But I dont know how to change the route. Please help me.
CakePHP has a fantastic documentation -> cookbook
And to your question, did you try to do what they suggested? Creating the class WorkspaceController in Controller!
In app/Config/routes.php:
Router::connect(
'/workspace/registration',
array('controller' => 'index', 'action' => 'index')
);
If /workspace/registration is where you register to join a workspace, though, I'd really recommend creating a WorkspaceController to deal with all workspace-related functionality.
Work with the default routes, not against them, it will make your life a lot easier.
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 have a cake php application and 4 or 5 pages contain static pages in .php, I have also built some controllers/models and views for those pages that need to be dynamic. My question is.. Can I have a view just with static code there and call it something like
http://myweb.com/app/someemptycontroller/staticview/
Can this be done and if so how do i need to set it up in terms of model and controller..(maybe empty) i am not sure as I am a newbie in this cake php world.
Thank you
CakePHP comes with a working pages controller, which can be used to serve up static pages.
Any view you place in /app/views/pages can be accessed like (say your view is hello.ctp):
http://myweb.com/pages/hello
If you don't like the pages controller showing in the URL, add a line like this to your app/config/routes.php file:
Router::connect('/hello', array('controller' => 'pages', 'action' => 'display','hello'));
Now your page can be accessed by:
http://myweb.com/hello
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.