我们使用tp或者yii2的时候,会将网站的前台和后台按照模块分组。yii2的高级模板已经帮我们划分好了,tp系列框架需要自己配置分组。那么laravel5该怎么划分这样的模块呢?
routes.php
<?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('/', 'Home\IndexController@index'); //前台路由组 Route::group(['namespace' => 'Home'], function(){ // 控制器在 "App\Http\Controllers\Home" 命名空间下 Route::get('/', [ 'as' => 'index', 'uses' => 'IndexController@index' ]); }); //后台路由组 Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function(){ // 控制器在 "App\Http\Controllers\Admin" 命名空间下 Route::get('/', [ 'as' => 'index', 'uses' => 'IndexController@index' ]); });
设置了命名空间,admin后台控制器,统一加了admin前缀。并且设置了默认访问的控制器,Home文件夹下的Index控制器的index方法。
Home\IndexController.php
<?php /** * author: NickBai * createTime: 2017/2/6 0006 上午 9:05 */ namespace App\Http\Controllers\Home; use App\Http\Controllers\Controller; class IndexController extends Controller { public function index() { return 'this is home'; } }
Admin\IndexController.php
<?php /** * author: NickBai * createTime: 2017/2/6 0006 上午 9:03 */ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; class IndexController extends Controller { public function index() { return 'this is admin'; } }
此时访问 domain(你的域名)即可访问前台,domain/admin 即可访问admin后台
以上这篇laravel 实现划分admin和home 模块分组就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持黑区网络。