We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Accessing subfolders directly (ignore routing) in Phalcon?

Hi, I often test things by dumping them into a subfolder of my website. Doing this in Phalcon will give me a: Controller handler class cannot be loaded error (as there likely won't be any controller of that name).

How can I (easily) bypass this (totally ignore Phalcon)? Basically telling Phalcon that: if you can't find this controller, go look for at subfolder and do nothing (let the server serve the pages as it normally would had Phalcon not been installed).

If you're using this htaccess (assuming you're using apache):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

it should be already like you said. The line RewriteCond %{REQUEST_FILENAME} !-d means "if the requested path is not an existing directory", the next line means "if the requested path is not an existing file".. So it is the other way around, the webserver looks for folders/files and if they can't be found it passes the path to phalcon.



15.1k
edited Jun '14

I have this:

RewriteEngine on
RewriteRule  ^$ public/     [L]
RewriteRule  (.*) public/$1 [L]

And when adding this: RewriteCond %{REQUEST_FILENAME} !-d

so I have this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  ^$ public/     [L]
RewriteRule  (.*) public/$1 [L]

makes no difference.

edited Jun '14

Sorry, I'm talking about the .htaccess in the /public folder, try and look if that line exists in that htaccess



15.1k
edited Jun '14

It reads:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

I seem to be having the issue in my root folder ...

Also tried changing REQUEST_FILENAME to REQUEST_URI, no difference.



15.1k
edited Jun '14

I protect the »app« folder by redirecting all requests to the »public« folder. But that breaks the build process of bower components when I have to move that folder into the public folder (for them to work properly).

One should make a more specific protection file for phalcon; only specifically protect the »app« folder and not everything else.

Do you need to access a subfolder in the root folder, or in the /public folder?

If it's in the root folder, the easiest solution would be to move the subfolder under the public folder, it should work there



15.1k

In the root folder. I only want my Phalcon framework to work from the public folder (acting as an isolated app). But have the root otherwise behave normally. As stated above; an htaccess file that ONLY protects the »app« folder would do.

I protect the »app« folder by redirecting all requests to the »public« folder. But that breaks the build process of bower components when I have to move that folder into the public folder (for them to work properly).

I'm not using bower, but what if you move the bower.json in the public folder, and build from there?



15.1k

That would probably do the job. Just don't like to alter my 'normal' paths, as managing projects in different ways make them more prone to errors.

But thanks, I'll see what I can do with the htaccess file to make it only protect the public folder.

edited Jun '14

My suggestion is to use public as your new root for public files, and (if you really need it) conditionally rewrite in the root .htaccess for files you want to have in the root folder

RewriteEngine on

#don't rewrite if the request is "folder_in_root_folder"
RewriteRule  ^folder_in_root_folder$ -    [L]
RewriteRule  ^second_folder_in_root_folder$ -    [L]

RewriteRule  ^$ public/     [L]
RewriteRule  (.*) public/$1 [L]

Not sure it will work, but it's one way of doing it

It's still not clear to me what you're trying to do, but one of the good things in phalcon is that you can do it any way you want.



15.1k
edited Jun '14

I want:

https://localhost/project/folder_not_found/

and

https://localhost/project/app/

to be redirected to /public

And

https://localhost/project/folder_found/

to just act normally (that is, serve the files as normally)



15.1k
edited Jun '14
var folder = $1;

if ( !folder || folder.name === 'app' )
{
     goto( '/public' );
}
else
{
     goto( folder );
}
edited Jun '14

And you can't put folder_found under /public?

Why do you need https://localhost/project/app/ to redirect? That folder shouldn't be in an url, do you mean rewrite? (it doesn't need to be rewritten either, it's used to collect the files to be included from public/index.php.,,)



15.1k
https://localhost/project/

is my root.

I »move« my root to /public using the htaccess file (that's what Phalcon suggest to protect the app folder).

But I still wan't my original root to behave like my root (basically ONLY protecting the app folder), so that I can dump in subfolders containing all sorts of stuff for testing purposes without having to bloat my public folder with irrelevant stuff.

You've said you want to be able to dump folders into the root, and that you don't want to muddy up public/. The simplest solution I can think of is to make a new folder, say test, that you then dump all your test folders in to. The paths would then be:

https://localhost/project/test/some_test_folder

This could be achieved by adding a single line to the .htaccess file in your root:

RewriteRule ^test - [L,NC]
RewriteRule  ^$ public/    [L]
RewriteRule  (.*) public/$1 [L]

You won't be able to add directly to the root, but since it's just for testing, this will at least give you a spot you can dump tests without needing to reconfigure a bunch of stuff each time.