laravel-5.3(2) 路由配置

第一步:

按照上一篇搭建好工程后可以看到框架默认的 welcome 默认视图;

一般的web 开发框架是MVC设计模式,那么我们现在创建自己的控制器和视图,CMD 进入到工程根目录执行

php artisan make:controller Admin/IndexController

成功显示:

Controller created successfully.

 

 

vim  /app/Http/Controllers/Admin/IndexController.php

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
  public function index(){
    echo "IndexController--indexaction";die;
  }
}

第二步:

在第一步中,我们创建了控制器,但是我们如何使用呢?也就是如何通过URL访问呢?如何配置呢?这就是本章要讲的路由配置;

从工程根目录routes文件夹中打开web.php文件,文件中存在一个 指向welcome欢迎页的默认配置。我们暂时不动它;

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
  return view('welcome');
});

Route::get('/index/index','Admin\IndexController@index');

保存后,在地址内输入虚拟域名(我的是http://localhost.laravel.com)+/index/index后,就可以看到 echo 输出的内容了。

这是简单的给大家演示下路由配置;

第三步:

分组路由配置

我们把刚才web.php中的路由配置先注释掉;

分组路由配置是group方法;

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function()
{
    Route::get('/index/index','Admin\IndexController@index');
});

打开浏览器输入域名+prefix+/index/index   (http://localhost.laravel.com/admin/index/index)

效果和上面的是一样的。

猜你喜欢

转载自www.cnblogs.com/nyfz/p/9295798.html
今日推荐