开发者

Understanding MVC architecture as non-MVC-developer

开发者 https://www.devze.com 2023-01-11 17:56 出处:网络
I have a problem with the understanding MVC architecture. It\'s not that I don\'t know anything about MVC. Everything makes sense to me in a MVC architecture but if I want to start to develop my app i

I have a problem with the understanding MVC architecture. It's not that I don't know anything about MVC. Everything makes sense to me in a MVC architecture but if I want to start to develop my app in an MVC architecture I'm stuck.

Basically there are a lot ways to do what you want in the programming world but I want to do it like it was supposed to be. So maybe there is someone who can help me out.

But here my recent problem with MVC:

I want to write my own blog in Ruby on Rails. This not a big deal I think. I would have my models like articles, comments, user and much more. For each of them I would create a controller to manage them and all. The problem is when it comes to the Admin-Panel. I want that an article can only created in the Admin-Panel.

So what should I do? Should I create a Admin-Panel controller to manage all those tasks which can only accomplished in the Admin-Panel at all? Otherwise I开发者_高级运维 think it is too much for a single controller.

I want that my urls looks something like this:

For Admin-Panel tasks: example.com/admin/article/create For Viewers: example.com/article/show

(I think restful Rails routes are looking different but I think you get what I want)

How would you accomplish this task in an MVC architecture and how should it be done? Can you help to understand those MVC tasks much better?

Thank you in advance.


The two things to keep in mind when making an admin area are 1) you can create namespaces for routes to get the /admin URLs you're looking for and 2) you can have controllers inherit from other descendants of ActionController

So to make an admin area, you'd want to have RESTful resources declared in a namespace (assumes Rails 3 routes):

# routes.rb
resources :users
resources :posts
resources :pages

namespace :admin do |admin|
  match '/' => 'dashboard#index'
  resources :users
  resources :posts
  resources :pages
end

The top set is the public ones and the bottom set gives you the admin routes like /admin/users/new and /admin/posts/1, etc. I'm also assuming you might want a "dashboard" so I'm setting up a route to the index method of an Admin::DashboardController

Then you create an admin base controller that descends from ApplicationController. Use it to hold your admin area layout and your authentication filters:

class Admin::BaseController < ApplicationController
  before_filter :require_user
  layout 'admin'
end

Now make a directory in app/controllers called "admin". Make controllers in there as normal, but have them inherit from your base controller:

# pages_controller.rb
class Admin::PagesController < Admin::BaseController
  # Controller code in here
end

Make a corresponding directory in app/views for "admin" and you're good to go -- everything is namespaced out and views/controllers would behave like you think.

You can always run "rake routes" to see all the admin routes.

Hope that helps!


What you're describing sounds like a good example of a Cross-cutting concern. Specifically, user authentication is a concern which affects many other concerns. In your case, user rights affects the posting of articles, but in the future it's reasonable to assume it could affect things like control of comments, addition of categories, etc.

In cases like this, it's best to keep logic in the controllers that address the primary concern (i.e. creating an article would fall under the article controller), and offload authentication and role security onto an additional module that your controllers can all use. This allows you to keep article code centralized in a single controller, and also allows you to add role-level security in other controllers in the future without significant refactoring.

There's a variety of plugins for authentication and role-level security in Rails. I'd do a little research and find one that suits your needs best.

0

精彩评论

暂无评论...
验证码 换一张
取 消