2

I'm developing a CMS using yii framework. There is a frontend and backend. I want the users to be able to access the backend like this: http://www.mysite.com/admin, right now it is working like this: http://www.mysite.com/admin.php.

For the backend I have defined different section with it's own config, controller and ... and the page for accessing the backend in admin.php

here is my directory structure:

...
admin
     --components
     --config
         ---main.php
     --controllers
         ---NewsController.php
         ---ShowCOntroller.php
         ---SiteController.php
     --models
         ---LoginForm.php
         ---News.php
         ---Show.php
         ---User.php
     --runtime
     ...
     --Views
        ---layouts
        ---news
        ---show
        ---site

protected

    -commands
    -data
    -extentions
    -messages
    -migrations
    -models
    -modules
       --image
    -runtime
    -views


themes
uploads
admin.php
index.php
.htaccess  

And here is my .htaccess file:

Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule admin admin\.php [T=application/x-httpd-php]

 # 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
</IfModule>
1
  • 1
    Normally a yii page has a route something like /index.php?r=some/thing . How can you have 'admin.php' page? Commented Jun 11, 2012 at 14:28

6 Answers 6

3

You can have a look of how did I solved with the help of @bool.dev Yii: .htaccess and urlManager for separate backend and frontend.

I hope it helps.

Sign up to request clarification or add additional context in comments.

9 Comments

Can you post your directory structure and the content of your .htaccess?
Ok, I just checked out and my directory structure was different, the backend section was inside the protected directory. I changed my directory structure to something like yours. Now I get another error. now it recognizes /admin instead of admin.php but it gives me this error: Error 404 - Unable to resolve the request "admin".
That is because Yii is trying to find an admin controller inside of protected. It would be easier if you post the content of your .htaccess and the sketch of your directory structure.
I changed the structure back to how it was because I want the admin section also be inside protected. I edited my post with my directory structure.
But why did you decide to put the backend out of protected area? Doesn't it cause a security issue?
|
2

One thing you could consider is creating the backend as a Yii module inside the frontend. A module is like an application within an application. You would create models, controllers and views, possibly reusing code from the frontend. The URLs for accessing the module would include the module name; for example http://www.example.com/admin/controller/action.

On the other hand, if you just want to map requests made to /admin go to the script admin.php, you can do it in mod_rewrite. For example:

# .htaccess
RewriteEngine On

RewriteRule ^admin admin.php     # route requests for /admin to admin.php

# Rest of Yii's rewrite rules:
# 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

2 Comments

I tried to use the rewrite_mode, but it didn't work. here is my htaccess: RewriteEngine on RewriteRule ^admin admin.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php
Depending on your setup you may need additional settings, like for example RewriteBase /.
2

I would also suggest going the module way.

Then you can use the URL manager to manage your URL for the admin.

1 Comment

Yes, I also decided to use modules for this. my approach is not very efficient...
1

You really need to direct all requests through index.php.

Use the URL manager to route admin requests. Have a look here, http://www.yiiframework.com/doc/guide/1.1/en/topics.url

Comments

1

I suggest you to use Yii Module base concepts. So the Admin page will be a module on your application.

Please see this link http://www.yiiframework.com/doc/guide/1.1/en/basics.module

1 Comment

The thing is that I'm new with Yii and that tutorial in your link is not very thorough. And I couldn't find any other tutorial which explains better how I should do this
0

Use yii Url Manager which is best at all and if all the configuration and controllers are different for the backend then you can build a controller with named AdminController.

2 Comments

I'm using UrlManager, but can you be more specific, about what I should define in UrlManager?
you can use different controller for backend like AdminCotroller and define their accessControl to actions for only admin users. On my way I will define a variable of user component which will identify the admin user. Then the admin user is accessible to admin controller actions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.