laravel 实现划分admin和home 模块分组

2020-10-20 14:46:26 浏览数 (1)

我们使用tp或者yii2的时候,会将网站的前台和后台按照模块分组。yii2的高级模板已经帮我们划分好了,tp系列框架需要自己配置分组。那么laravel5该怎么划分这样的模块呢?

routes.php

代码语言:javascript复制
<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

//默认控制器
Route::get('/', 'HomeIndexController@index');

//前台路由组
Route::group(['namespace' =  'Home'], function(){
 // 控制器在 "AppHttpControllersHome" 命名空间下
 Route::get('/', [
 'as' =  'index', 'uses' =  'IndexController@index'
 ]);

});

//后台路由组
Route::group(['namespace' =  'Admin', 'prefix' =  'admin'], function(){
 // 控制器在 "AppHttpControllersAdmin" 命名空间下

 Route::get('/', [
 'as' =  'index', 'uses' =  'IndexController@index'
 ]);

});

设置了命名空间,admin后台控制器,统一加了admin前缀。并且设置了默认访问的控制器,Home文件夹下的Index控制器的index方法。

HomeIndexController.php

代码语言:javascript复制
<?php
/**
 * author: NickBai
 * createTime: 2017/2/6 0006 上午 9:05
 */
namespace AppHttpControllersHome;

use AppHttpControllersController;

class IndexController extends Controller
{
 public function index()
 {
 return 'this is home';
 }
}

AdminIndexController.php

代码语言:javascript复制
<?php
/**
 * author: NickBai
 * createTime: 2017/2/6 0006 上午 9:03
 */
namespace AppHttpControllersAdmin;

use AppHttpControllersController;

class IndexController extends Controller
{
 public function index()
 {
 return 'this is admin';
 }
}

此时访问 domain(你的域名)即可访问前台,domain/admin 即可访问admin后台

以上这篇laravel 实现划分admin和home 模块分组就是小编分享给大家的全部内容了,希望能给大家一个参考。

0 人点赞