我们使用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后台
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理