1

I am having problems trying to get my cake application to display items from the database on a single page based on the url of the page... For example I want to display items that are in the "Party" category and I type "mydomain.com/All/party" in the address bar and I get a list of all the items in that category on that page.

This is my code but the pages I am getting from the route is blank:

My routes.php:

    Router::connect('/categories/:name', array(
   'controller' => 'All', 'action' => 'categories'
),
array(
    'pass' => 'catname',
    'catname' => '[a-zA-Z0-9]+'
));

my AllController.php:

    function categories($catname = null) {
$options = array(
    'conditions' => array('Category.name'=>$catname)
);
$Category = $this->Category->find('all', $options);
$this->set('Category', $Category);
$this ->render('/All/categories');
}

Any help would be appreciated.

  • 4
    what is this `cakephp-appmodel` tag for and why are you systematically replacing `mvc` with that tag @tereško (Yes I've read your numerous "CakePHP is not (my interpretation of) MVC" posts/comments but it is a framework implementing [Model–View–Controller (MVC) is an architectural pattern used in software engineering](http://stackoverflow.com/questions/tagged/mvc)). I don't think you are helping anyone by retagging things like this. – AD7six Dec 03 '13 at 15:31
  • 4
    @AD7six the `cakephp-appmodel` tag is for questions where people struggle with Cake's god-awful active record implementation. And the `mvc` tag was removed, because **the question** had nothing to do with it. Just because you like to think that you favorite framework claims to implement something, does not mean that all questions involving that "framework" should also contain the excerpt from your marketing materials. – tereško Dec 03 '13 at 16:41
  • 1
    @tereško tbh I don't care at all about the `mvc` tag, I do care however that you're retagging things with `cakephp-appmodel` I've [posted on meta](http://meta.stackexchange.com/questions/209759/merge-cakephp-appmodel-and-cakephp-model) to hopefully get the tag merged/renamed with `cakephp-model` even though I don't see much value in that tag either. – AD7six Dec 03 '13 at 16:43
  • 4
    "AppModel" is the name that you people have given it. http://api.cakephp.org/2.3/class-AppModel.html .. or maybe we should rename it to `cakephp-object`? – tereško Dec 03 '13 at 16:46
  • 1
    @tereško you repeatedly demonstrate that you don't know and don't like CakePHP - that's fine; If you want the `mvc` tag to be [your corner of the internet](http://stackoverflow.com/users/727208/teresko?tab=activity&sort=revisions) that's also fine - but the tag `cakephp-appmodel` needs to burn with fire as it's misleading. – AD7six Dec 03 '13 at 16:49
  • 2
    @AD7six it's not about being _his_. It's simply incorrect to suggest it implements MVC, or that the question relates to that tag. What it does is implement a pattern called [Model2](http://en.wikipedia.org/wiki/Model_2). I don't understand why you're complaining about him removing questions that relate to completely different patterns and frameworks... May I suggest http://martinfowler.com/eaaDev/uiArchs.html ? – Benjamin Gruenbaum Dec 03 '13 at 16:52
  • 1
    @BenjaminGruenbaum I'm complaining because this tag `cakephp-appmodel` is meaningless. `cakephp-object` would be equally meaningless or infact worse since object is a different class. I don't care about removing the mvc tag because it doesn't add any value to CakePHP questions - nobody is going to be looking for Cake questions in that tag, nobody answering Cake questions gets extra information from seeing it either. – AD7six Dec 03 '13 at 16:54
  • 2
    @AD7six Well I'm glad we all agree on [tag:mvc] being unwanted here. I was also replying to your suggestion it implements MVC - it's doesn't, and there is no shame in that, it's perfectly fine not to implement MVC it doesn't really fit the web very well. To dispute your MVC claim I linked to an article by Fowler that shows it. Anyway - this discussion really doesn't belong in the comments, if you'd like you can usually find teresko in the stack overflow php chat room where you can agree on something. – Benjamin Gruenbaum Dec 03 '13 at 17:00
  • @BenjaminGruenbaum fwiw [this is not CakePHP](http://en.wikipedia.org/wiki/File:JSP_Model_2.svg). In CakePHP, views do not talk to models. – AD7six Dec 03 '13 at 17:08
  • @AD7six To be completely honest I'm not sure about the name of the pattern CakePHP implements - model2 was the closest. It's definitely not MVC though. Not implementing MVC is probably good though because it doesn't work very well in the server-side. – Benjamin Gruenbaum Dec 03 '13 at 17:18

3 Answers3

2

I don't get at all the code you just posted, but what you want to achieve is quite trivial:

Router::connect(
    '/products/:category', 
    ['controller' => 'products', 'action' => 'categorized'], 
    ['pass' => ['category']]
);

In ProductsController:

function categorized($category) {
    $conditions = ['Category.name' => $category];
    $this->set('products', $this->Prodcut->find('all', compact('conditions')));
}

That's it. You may want to reuse the index action if you don't need a separate view template for that, in that case just set the conditions (for example to the Paginator component) and call $this->setAction('index')

AD7six
  • 63,116
  • 12
  • 91
  • 123
  • I am getting an Error: syntax error, unexpected '[' I made some changes to the code you sent as well as to my opening post, I should have made the url that I want to get clearer... though I dont think that is the problem. The code i edited: routes.php: Router::connect( '/All/:category', ['controller' => 'All', 'action' => 'categories'], ['pass' => ['category']] ); AllController: function categories($category) { $conditions = ['Category.name' => $category]; $this->set('products', $this->Product->find('all', compact('conditions'))); } – Horace Mozadegh-Khorampast Cun Dec 03 '13 at 17:48
  • I used PHP 5.4 short array syntax, you can change accordingly for your PHP version – José Lorenzo Rodríguez Dec 03 '13 at 20:50
0

I found a much easier way to do what I wanted. I just remember that I had a database search option that allows persons to search for products and categories. What I did was simply do a search for the categories and then copy the URL of the page I got and set that as a link to the category in my application.

Example code:

<ul class="filter-options">
  <li><a style="color:#fff" href="http://mydomain.com/products/search?q=parties">Parties</a></li>
  <li><a style="color:#fff" href="http://mydomain.com/products/search?q=festivals">Festivals</a></li>
  <li><a style="color:#fff" href="http://mydomain.com/products/search?q=theatres">Cinemas/Theatres</a></li>

0

Everything looks good on your side just change your routes.php as follows

Router::connect('/categories/:catname', array(
        'controller' => 'all', 
        'action' => 'categories'),
            array('pass' => array('catname'),
                'catname' => '[a-zA-Z0-9]+'));
Nabila
  • 16
  • 3