cakePHP: having model named 'Page' - cakephp

I'm going to create models named 'Book' and 'Page' which are related. How can I avoid conflicts between my 'Pages' and built-in 'Pages' of cakePHP inside View folder?
I should notice that I need built-in Pages of cakePHP to display some static pages such as contact us

You can just rename the default PagesController to something else like StaticPagesController (and related changes to routes and view folder). Then make a new PagesController & Page model.
You can even the existing PagesController if you wanted to by removing public $uses = array(); from it. But since your book pages and static page rendering are unrelated it's best not to do so.

Related

Creating static home page for CakePHP3

I am working on a CakePHP3 project. I want a static homepage that will be loaded on www.mysite.com.
For this I have created a PagesController which will handle all the static pages in the website like about,contact,etc.
I am having display.ctp view in Template/Pages/display.ctp to load on www.mysite.com.
But, for testing (routes are not configured yet), I'm using www.mysite.com/pages and www.mysite.com/pages/display to show the view but it gives error as
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mysite.pages' doesn't exist
Do I need to create Table for this ?
It is much, much easier than that
For this I have created a PagesController
There is already a pages controller for serving static content, and a
static template for the home page, there is no need to create/overwrite the default pages controller, to replace it with the same (or less) functionality. This is also mentioned in the documentation
The steps to modify static pages (with default routes) are:
edit src/Template/Pages/home.ctp - look at the url /
create/edit src/Template/Pages/something.ctp - look at the url /pages/something
The error means that the application is looking for a model named Page. To tell the application that your controller does not refer to any model you have to use something like bellow. Also add the proper action. www.mysite.com/pages/display means in controller "pages" call action "display".
class MyController extends AppController {
var $uses = false;
public function display {}
}

CakePHP: Flexible redirection for views and controllers

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));

How can I deny test.ctp and allow home.ctp to anonymous users in CakePHP?

I'm using AuthComponent in CakePHP 2.
I want to show to anonymous users just the 'home' view and deny 'test' view.
Both views are inside the folder Views/Pages
If I use this code:
public function beforeFilter(){
parent::beforeFilter(); //calling parents before filter
$this->Auth->allow('home'); //allowing home only for pages controller
}
in my PagesController I'cant access any page of my PagesController, including home.ctp.
If I use:
$this->Auth->allow('display')
in my PagesController I can access any page of my PagesController, including test.ctp.
How can I solve this issue?
I tried to create a new method on my PagesController name home(). But without success.
Here is a good answer for you.
Allowing a Specific Page in Cakephp
Keep in mind that the Auth->allow is for methods. Display is the method in the pages controller. The Home page is considered a variable used in display. So you will have to do this programmatically in the Pages controller.
Situation one: If you dont want to utilize the default 'display' function of CakePHP.
Along with creating a new method in PagesController named home(),
you must also alter the code in the file app\Config\routes.php as shown below to make it work for home and not for other methods, this new code redirects all queries to pages controller to home.ctp.
//default code statement:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
//New code
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'home'));
Situation two: If you want to keep the default 'display' function of CakePHP
Then you must build some redirect logic in display function when visitor is accessing test.ctp page, as also answered by #Chuck Burgess with a link.

adding a view just with static data

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

cakePHP project, orgazization of folders, pages, functions

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.

Resources