开发者

CakePHP - Image Gallery Application finished, now how to write routes for /:album_name?

开发者 https://www.devze.com 2023-03-24 14:07 出处:网络
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.

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));
开发者_C百科            $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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消