0

How can I use /album_name and /album_name/image_name for paths in my gallery? I already have the site working, but paths are /album, /image with index() and view() functions at the moment.

<?php
class Image extends AppModel {
        var $name = 'Image';
        var $displayField = 'Name';
        var $belongsTo = array(
                'Album' => array(
                        'className' => 'Album'
                )
        );
}
<?php
class Album extends AppModel {
        var $name = 'Album';
        var $displayField = 'name';

        var $belongsTo = array(
                'ParentAlbum' => array(
                        'className' => 'Album',
                        'foreignKey' => 'parent_id'
                )
        );

        var $hasMany = array(
                'ChildAlbum' => array(
                        'className' => 'Album',
                        'foreignKey' => 'parent_id',
                        'dependent' => false
                ),
                'Image' => array(
                        'className' => 'Image',
                        'foreignKey' => 'album_id',
                        'dependent' => false
                )
        );
}

<?php
class ImagesController extends AppController {

        var $name = 'Images';

        function index() {
                $this->Image->recursive = 0;
                $this->set('images', $this->paginate());
        }

        function view($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid image', true));
                        $this->redirect(array('action' => 'index'));
                }
                $this->set('image', $this->Image->read(null, $id));
        }

}
[root@directadmin01 public_html]# cat controllers/albums_controller.php
<?php
class AlbumsController extends AppController {

        var $scaffold = 'admin';

        var $name = 'Albums';

        var $paginate = array(
        'limit' => 95,
        'order' => array(
        'Album.name' => 'asc')
        );

        function index() {
                $this->Album->recursive = 0;
                $this->set('albums', $this->paginate());
                $this->set("title_for_layout","Albums");
        }

        function view($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid album', true));
                        $this->redirect(array('action' => 'index'));
                }
            }
            $this->set('album', $this->Album->read(null, $id));
            $this->set("title_for_layout", $this->Album->data['Album']['name']);
        }
}

This is my first Cakephp application, so please don't be to rough on me. I am impressed by the fact that an entire (simple) gallery site can be written in so few lines.

However it seems that creating /album_name/image_name style routing could be tricky?

It should be somewhat like

    Router::connect('/:album_name', array('controller' => 'albums', 'action' => 'view', 'home'));
    Router::connect('/:album_name/:image_name', array('controller' => 'images', 'action' => 'view', 'home'));

But how can this ever be realized? Album_names in the database have multi-cases and spaces, while I'd want dashes and lowercase urls.

Dennis
  • 2,866
  • 7
  • 32
  • 49

1 Answers1

0

it's a little more complicated than just routing, you'll want to read about slug and cakephp. Have a look here: CakePHP - How to do reverse routing with slug?

Community
  • 1
  • 1
Anh Pham
  • 5,431
  • 3
  • 23
  • 27
  • When id's are involved, it's easy. But I preferably don't want to have id's in the paths. – Dennis Aug 02 '11 at 23:15
  • then your ablum names must be unique, the same for images in the same album. You don't have to follow exactly what they did, but it should point you to the right direction. – Anh Pham Aug 02 '11 at 23:38