Configure application for front-end and back-end

I read this tutorial: http://www.yiiframework.com/doc/cookbook/33/

But I’m not sure if this is precisely what I’m after.

Basically what I want is for the front-end + all links to be configured as such:

/yiiapp/index.php

/yiiapp/about.php

/yiiapp/contact.php

/yiiapp/admin/index.php

/yiiapp/admin/login.php

/yiiapp/admin/user/create.php

etc…

How can I acheive this? Just after a basic idea on how to do it.

After setting url type to path in both configs, you have to add a line to your .htaccess:




RewriteRule ^admin backend.php



Also set urlSuffix to ‘.php’. Hope, this will work.

I was hoping there would be a way of doing this without needing another entry script?

see here

http://www.yiiframework.com/forum/index.php?/topic/7365-what-do-you-think-of-this-files-structure/

I have no idea how to create module, is there a guide somewhere?

yiic shell ../index.php

>>module backend

in config/main.php




 'modules' => array('backend'),



>>model post

>> crud Post backend/Post

http://domain/app/backend/post

I think that using of two entry scripts makes things more clear. Because otherwise you’ll have to play with GET params and add some logic to index.php (e.g. which config to choose).

Anyways, try this:




RewriteRule ^admin index.php?admin



About admin module… My humble opinion is: the backend part is a self-sufficient application and it shouldn’t be a module of the frontend. Both parts have different configs, controllers, views, but share models.

But I think an admin module is a good idea too, at least, for not large applications.

andy_s - how would I configure all backend url’s to reference backend.php rather than index.php?

Also how do I omit the ‘site’ part of the URL when using CMenu widget? For example I want all URLs to be in the format of

[color="#000080"]/yiiapp/contact[/color] instead of [color="#000080"]yiiapp/site/contact[/color]

I already have [color="#000080"]RewriteRule . index.php[/color], if I add the rule above it does not work.

In your case you’ll need url rules:




'urlManager'=>array(

    'urlFormat'=>'path',

    'showScriptName'=>false,

    'urlSuffix'=>'.php',

    'rules'=>array(

        'admin'=>'site/index',

        'admin/<_c>'=>'<_c>',

        'admin/<_c>/<_a>'=>'<_c>/<_a>',

    ),

),



Yes, I know that’s not very beautiful. Also, I don’t use url manager in the backend part, because no one cares about beautiful urls there.

Use UrlManager rules again…

.htaccess:




Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on


# Make the backend accessible via url: http://site/admin.

RewriteRule ^admin backend.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



When you’ll try to access http://site/admin then backend.php will be executed. Isn’t it?

Yes you’re right - I was putting that rule at the bottom of the htaccess!

Thanks!

Just a slight issue I came across. If I navigate to: /webapp/backend.php

I get a CHttpException: Unable to resolve the request "site/error".

Any idea what might be causing this?

(I came across this issue after trying to log in at /webapp/admin/login)

EDIT: I managed to fix the above error. However it seems that logging in at [color="#000080"]/webapp/admin/login[/color] redirects me to [color="#000080"]/webapp/backend.php[/color] after logging in. Have you got any idea why?

In your backend controllers you should have a controller "SiteController" with action index, just like in the frontend part. It is the main page of your backend.

Seems it is the only case when this happens. If you’ll try to directly go to the login action from the frontend, you’ll be then redirected to backend.php (since nothing is stored in the returnUrl). Try to go to /webapp/admin. Here you will be redirected to login action and after logged in you’ll get back to /webapp/admin.

/webapp/admin just calls actionIndex which renders admin/index.php

Do you mean I should enforce actionIndex to redirect to webapp/admin/login?

I just thought you denied access to all controllers’ actions (except site/login), if a user is not logged in. So if he will try to access site/index, he will be redirected to login page.