1
votes

I'm having a hard time figure out how to paginate in cakePHP when i have activate admin routing.

I have localhost/myapp that is the main page and localhost/myapp/admin that is tthe admin area. So I want to paginate on the fron end, so in the localhost/myapp index page.

So as the main index page is index of Typology Model. in my app/Config/routes.php i have this routing for homepage:

Router::connect(
  '/', 
     array(
       'admin'=>false,
       'controller' => 'typologies', 
       'action' => 'index', 'index'
    )
);

PS: i Also Use slug for finding posts.

In my app/Controller/TypologyController I have this:

$this->Paginator->settings =  array(
    'joins' => array(
        array(
            'table' => 'items',
            'alias' => 'Item',
            'type' => 'LEFT',
            'conditions' => array('Item.id = Typology.item_id')
        )
    ),
        'conditions' => array(
            'Item.published' => 1, 
            'Typology.published' => 1
        ),
        'limit' => 1,
        'order' => array('Typology.sort' => 'ASC'),
        'fields' => array('Item.*', 'Typology.*'),  
        'recursive' => 2
);



$typologies = $this->paginate('Typology');
$this->set(compact('typologies'));

I have set limit=>1 just for testing purposes. In my View/Typologies/index.ctp i have this:

<?php
    $result = ''; 
   foreach ($typologies as $typology): 
         $id                    = h($typology['Typology']['id']); 
         $item_id               = h($typology['Typology']['item_id']); 
         $title                 = h($typology['Typology']['title']); 
         $description           = h($typology['Typology']['description']);     
         $thumbnail             = h($typology['Typology']['thumbnail']); 
         $price                 = h($typology['Typology']['price']); 
         $typology_category_id  = h($typology['Typology']['typology_category_id']);     
         $typology_condition_id = h($typology['Typology']['typology_condition_id']);                  

                    if (strlen($description)>330) {
                        $short_description = substr($description, 0, 327);
                        $description = $short_description."...";
                    }
                     $dir = "img/uploads/typology/thumbnails/";
                    if (file_exists( $dir . $thumbnail)) {
                      $typologyThumbnails = $dir . $thumbnail; 
                    }
                    else {
                      $typologyThumbnails = "img/uploads/noimg.jpg"; 
                    }
                    if($thumbnail=='NULL' || $thumbnail=='') {
                      $typologyThumbnails = "img/uploads/noimg.jpg";  
                    }           
                        $result .= "<div class=\"item_shadow\">";
                        if ($logged_in) {
                            if ($typology['Typology']['published']==0 || $typology['Typology']['published']==false || $typology['TypologyItem']['published']==0 || $typology['TypologyItem']['published']==false) { 
                                $result .= "<div class=\"badge red\"> non publicato </div>";
                            } 
                        } 
                        $result .= "<div class=\"item\" style=\"background-image:url({$typologyThumbnails});\">"; 
                        $result .=      "<div class=\"item-content\">";
                        $result .=          "<div class=\"item-top-content\">";
                        $result .=              "<div class=\"item-top-content-inner\">";
                        $result .=                  "<div class=\"item-top-title\">";
                        $result .=                      "<h4>{$title}</h4>";
                        $result .=                  "</div>";
                        $result .=              "</div>"    ;
                        $result .=          "</div>";
                        $result .=          "<div class=\"item-add-content\">";
                        $result .=              "<div class=\"item-add-content-inner\">";
                        $result .=                  "<div class=\"description-inner\">";
                        $result .=                      "<p>{$description}</p>";
                        $result .=                  "</div>";
                        $result .=                  "<div class=\"read-more-inner\">"; 
                        $result .=                  $this->Html->link("maggiori informazioni". $this->Html->image('elenco.png'), array('admin'=>false,'controller' => 'items', 'action' => 'view', 'slug' => Inflector::slug($typology['TypologyItem']['seo_url'],'-'),'id'=>$typology['Typology']['item_id'],'?'=> array('active_tab' => $typology['Typology']['id'])), array('escape' => false));
                        $result .=                  "</div>";
                        $result .=              "</div>";
                        $result .=          "</div>";
                        $result .=      "</div>";
                        $result .=  "</div>";
                        $result .= "</div>";                   

        endforeach; 
           $result .= "";      
           ?>   

So as you see first i store all the data into a variable then i echo it like this with the paginator helper:

<?php 
    if (isset($result)) {
         echo $result;
    }    
?>
<p>
<?php
    echo $this->Paginator->counter(array(
    'format' => __('Page{:page} di {:pages}, Showing {:current} records of {:count} in total, starting at {:start}, and finishing at {:end}')
    ));
?>  
</p>
<div class="paging">
<?php
    echo $this->Paginator->prev('< ' . __('Preview'), array(), null, array('class' => 'prev disabled'));
    echo $this->Paginator->numbers(array('separator' => ''));
    echo $this->Paginator->next(__('Next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>

So the first time i load the page, URL is like this http://localhost/mycakephpApp/ it displays one date like it suppose to: Page load Paggination

When i click next, or number 2, URL is like this: http://localhost/mycakephpApp/typologies/index/index/page:2, it should display the next one. It does but now it is blank like this. Second page paggination

I don't know what am i missing or what am i doing wrong. Does anyone has any idea how to fix this? I would really appriciate your your help.

1
You set index parameters in your routing options 'action' => 'index', 'index' , try without it eg:Router::connect('/', array(admin'=>false, 'controller' => 'typologies', 'action' => 'index'));Salines

1 Answers

2
votes

Relative urls are problematic

The view contains the following:

$result .= "<div class=\"item\" style=\"background-image:url({$typologyThumbnails});\">";

Where $typologyThumbnails is a relative url of the form:

img/uploads/typology/thumbnails/whatever.jpg

That means that for the url http://localhost/mycakephpApp/ the following image url is requested:

http://localhost/mycakephpApp/img/uploads/typology/thumbnails/whatever.jpg

However, for the url http://localhost/mycakephpApp/typologies/index/index/page:2 the following image url is requested:

http://localhost/mycakephpApp/typologies/index/index/img/uploads/typology/thumbnails/whatever.jpg

Which doesn't exist.

There's a very simple solution:

  • Always use absolute urls i.e. /img/uploads/typology/thumbnails/whatever.jpg
  • Use helper functions and the router class, or at least recognise what they do.

You can fix the problem as asked by doing the following before using the variable:

$typologyThumbnails = Router::url('/' . $typologyThumbnails);

This will output:

/mycakephpApp/img/uploads/typology/thumbnails/whatever.jpg

Use helpers and elements

View files of the form:

echo "large chunk of html"

are rather hard to read/maintain. Instead the view code should be for example:

<?php
foreach ($typologies as $typology) {
    echo $this->element('typology', array('row' => $typology));
}

// pagination links

And the body of the foreach loop in an element. But don't write large swathes of html as strings - it's a lot easier to read/maintain if it's written as:

<?php
// /App/View/elements/typology.ctp

// variable setup
?>
<div class="item_shadow">
  <div class="item" style="background-image:url(<?= Router::url($typologyThumbnails) ?>);">
    ...
    <h4><?= h($row['Typology']['title']) ?></h4>

It also has the added benefit that your editor will highlight the html.