0
votes

I try to extend tutorial Simple ACL controlled application (enter link description here). I want add table Categories so tables are:

CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
password CHAR(40) NOT NULL,
group_id INT(11) NOT NULL,
created DATETIME,
modified DATETIME
);


CREATE TABLE groups (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
created DATETIME,
modified DATETIME
);


CREATE TABLE posts (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id INT(11) NOT NULL,
category_id INT(11) NOT NULL,
title VARCHAR(255) NOT NULL,
body TEXT,
created DATETIME,
modified DATETIME
);

CREATE TABLE categories (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
);

Now I have add.ctp, edit.ctp, index.ctp and view.ctp in app/View/Posts and in app/View/Categories. These methods are accessible after login. But now I want display some menu and posts by categories for non logged users. After click on Category1 in main menu it will display only posts from Category1. Something like this:

Main menu: **Category1** | Category2 | Category3

  Title of post1 in Category1
  Body of post1 in Category1

  Title of post2 in Category1
  Body of post2 in Category1
  ..........

Main menu: Category1 | **Category2** | Category3

  Title of post1 in Category2
  Body of post1 in Category2

  Title of post2 in Category2
  Body of post2 in Category2
  ..........

I don't understand how to do that. Add some .ctp files to app/View/Posts? Or something else? Thank you for help.

1

1 Answers

1
votes

This will allow all methods typed in controller to be available for non-logged users.

in PostsController:

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow(array('methods','in-my-controller','which-will-be-allowed-for-not-logged'));
}

Edit:

public function yourmethod($categoryId = null) {
    $posts = $this->Post->find('all', array(
        'conditions' => array(
            'Post.category_id' => $categoryId
        )
    ));
    $this->set(compact(posts));
}

and access in browser page.com/posts/yourmethod/2

in view file you will get variable $posts for access data.

Of course it is simplified as much as I could make it.