1

I'm looking for suggestions on how to "separate" a front-end and back-end without too much added complexity. The purpose is for a blog application (I know others exist, I just want to roll my own). A couple simple index/view views for a front-end with more complex (index, create, update) views on the back-end.

For example, I'm am currently building a website using the advanced template and it's working as desired: different layouts for the front-end and back-end admin area, navigating to "/admin" takes you to the admin area, etc.

However, I don't need separate authentication, don't really care about separate controller logic, don't need a "view" action on the back-end or a "create" on the front-end. Though, it would be nice to have the URL include the "/admin" prefix for those administrative functions

Is there some easier way to give myself the experience of a separate admin area without going through the added complexity of separating backend/frontend/common apps/configs?

1 Answer 1

4

The simplest way of doing that is to create a module entitled admin and set up some access rules on it.

Even simpler is to create a controller called admin..

What you choose, depends on how many admin actions you need.

The advanced application template is indeed going to be overkill for the majority of Yii applications.

A careful mix of RBAC and module/controller magic will get you where you want to go, I hope. ;)

Edit:
I recommend integrating as much admin functionality into your application (views) as possible.

if (\Yii::$app->user->can('whatever')) {
    // do whatever
}

That way, the amount of stuff that the admin has to manage in the pseudo-backend can be shaved down considerably. :)

See: Yii Guide - Role Based Access Control (RBAC)

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

5 Comments

Thanks for the thought. I had considered modules (forgot to mention it in my question) but thought perhaps there would be something else with less separation. In other words, I want to use the same models for front- and back-end. Now I'm thinking that perhaps I could reuse the "front-end" models in the back-end but I'll have to give it a try. Modules may make it easier to separate the layout/style of the admin as well...
@justinvoelker in a project of mine, I have all my models in 'common' and even the layout is shared. So the amount of stuff in the backend is truly minimal :) It is basically a place to hold a couple of third-party modules for rbac admin and user admin.
That sounds good @jacmoe. One set of models, different views for front- and back-end, the "/admin" in the URL, a separate layout and assets (css, js)... I think modules might work after all. Perhaps I just needed your answer to force me into rethinking about modules!
Had a chance to test out this sort of implementation and it will work exactly as I need it. To get the separation I needed I created a module, copied AppAsset asset bundle to AdminAsset and changed the css file it included, copied layout/main.php into my module's views directory, changed main.php to register AdminAsset instead of AppAsset, and set 'layout' => 'main' in the module config (it's null for modules by default). Back-end and front-end look totally different, only one set of models, controllers/views are separated to only contain the items they need. Perfect!
@justinvoelker that is neat :)

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.