PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] How could I routing to the app/webroot folder? (CakePHP)

 April 22, 2022     cakephp, cakephp-2.3, php     No comments   

Issue

If I type this: "http://examplepage.com/gallery/examplecagegory/1-test-picture.jpg" to the browser.

Go to webroot: "app/webroot/gallery/pictures/1.jpg"

I tried:

Router::connect('/gallery/:slug_category/:id-:slug.:extension',
                array('webroot/gallery/pictures'),
                array(
                    'pass' => array('id', 'slug'),
                    'id' => '[0-9]+'
                    )
                );

But I stucked in the second row... :-/


Solution

This is not something you can do with routing, as the book states:

Routing is a feature that maps URLs to controller actions.

An image resource is not a controller action. You should just use a plain RewriteRule in the .htaccess file in app/webroot to rewrite all calls. Something like this should do the trick:

RewriteRule ^gallery/[a-z]+/([0-9]+)-[a-z-]+\.([a-z]{3})$ /gallery/pictures/$1.$2

Please do be aware that the HtmlHelper searches for images in the app/webroot/images folder by default, so you will need to use absolute URLs (prefix all image calls with a leading slash) to use your rewritten path, for example this will not work:

$this->Html->image('gallery/examplecategory/1-test-picture.jpg');

You should use this instead:

$this->Html->image('/gallery/examplecategory/1-test-picture.jpg');


Answered By - Oldskool
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing