Generally splitting admin and web app into two ent

2019-05-30 06:36发布

问题:

I am building a public facing web/mobile application that will have a substantial admin controlled back end. Although this question is quite general, I am using CakePHP to build the application.

I am looking into whether it would be wise to split the admin and public applications into two applications. Both applications would use the same database. The main reason I am looking into this would be for improved security, but also portability of the front end.

I have also thought about developing a CakePHP based RESTful API that both the front and back end would share.

Would an API be the best way to go about this or should each application simply only share the database, or is splitting the applications just creating more work in the long run?

回答1:

I think it's best to keep both the Admin functionality and REST API in your main CakePHP application. (You don't specify a version, but I'm assuming since you're making a new app you're using 2.0. It has some benefits below.)

As mark mentioned you can do something called prefix routing that allows you to create special actions that only admins can use in your existing controllers. There's a full explanation in the Prefix Routing docs.

The gist is that you specify the prefix you want in core.php:

Configure::write('Routing.prefixes', array('admin'));

So going to /admin/users/edit/5 would call the method admin_edit of our UsersController passing 5 as the first parameter. The view file used would be /views/users/admin_edit.ctp.

You can set a default "admin" homepage in routes.php:

Router::connect('/admin', array('controller' => 'pages', 'action' => 'index', 'admin' => true));

As for the REST API, if you're using 2.0 this is a built-in that is pretty easy to turn on. There's a good intro on the REST page.

Activating it just requires adding these lines to routes.php:

Router::mapResources('recipes');
Router::parseExtensions();

This sets up some default REST routes:

#HTTP format URL.format              Controller action invoked
GET          /recipes.format         RecipesController::index()
GET          /recipes/123.format     RecipesController::view(123)
POST         /recipes.format         RecipesController::add()
PUT          /recipes/123.format     RecipesController::edit(123)
DELETE       /recipes/123.format     RecipesController::delete(123)
POST         /recipes/123.format     RecipesController::edit(123)

There's more info in the doc so please check it out.



回答2:

I don't think it makes sense to split the application into two separate ones. it can easily be accomplished that user frontend and admin backend are separated completly using an "admin" prefix.

So all user actions

/
/controller/
/controller/action

etc

all admin actions

/admin/
/admin/controller/
/admin/controller/action

and so on