Yii Routing

Hi,

I have directory ‘upload/download’ in my project root. And I add controller/action - UploadController and actionDownload on it.

What I want is when user hit the url ‘/myproject/upload/download’ to execute controller action instead to open directory ‘upload/downlpoad’. I have to keep the same names.

My structure is:

  • project root

  • ->upload

  • –>download

  • ->protected

  • –>controllers … and so on.

I try with urlManager but without success.

I am not shure is it possible or how to do it?

This is probably something that needs to be done in a .htaccess file. If you only allow direct access to files (not directories), then Yii’s routing should kick in automatically.

Can you show your existing .htaccess file?

I have only this in my .htaccess file




Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on

# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php

RewriteRule . index.php



Try removing this condition:




RewriteCond %{REQUEST_FILENAME} !-d



When I remove it everything crashes.

None of the links work.

Even jquery is not loaded.

Ok, it’s working when I remove the condition but application can’t find jquery file I try to load.

Okay, put that back in :D .

Your best course of action is probably to put a separate .htaccess file inside the upload/download directory. I’ve done something similar for on demand thumbnail generation.

You’ll need to experiment with the content of the file. Something like this might work:




Options -Indexes


<IfModule mod_rewrite.c>


RewriteEngine on

RewriteBase /


# If the requested file does not exist...

RewriteCond %{REQUEST_FILENAME} !-f


# forward to index.php

RewriteRule . index.php


</IfModule>



I’m not too familiar with .htaccess rules though, so I suspect you’ll need to do some more work on it.

The problem was that I load jquery with ‘registerCoreScript’. I change it to load from my file I put in application and now works. I don’t now why Yii::app()->clientScript->registerCoreScript(‘jquery’) doesn’t work.

I’d be concerned that other things will break if something so central to Yii does.

Yeah, you are right. I will try to do it with separate .htaccess file and see what will happen.

To expand on something said above: I think the problem is that you have a directory AND a controller called ‘upload’ The .htaccess first looks to see is the user is requesting a file or directory that exists…THAN sends off to index.php.

Try changing the name of your ‘upload’ directory, or the structure to something like




root

  +-assests

  +-files

  | +-upload

  | +-download

  +-protected

  + etc 



Another option that I have seen quite often is to store the files in protected then use a proxy action to send to user. The link tag would call the controllerId/actionId passing an identifier to which file to send.