laravel强大功能路由初探(二)

 

目标当然是先输出helloworld

配置hosts文件和apache下的httpd-vhosts.conf,

hosts:127.0.0.1  www.blog.com

httpd-vhosts.conf:

<VirtualHost *:80>
  DocumentRoot "D:\www\htdocs\blog\laravel\public"
  ServerName www.blog.com
</VirtualHost>
以下代码 均在routes.php里面操作

 

//基础路由1
Route::get('/',function(){ return 'helloworld'; });

输出如下:

//基础路由2
//不能直接输入post方法访问路由
Route::post('test1',function(){ return 'post'; });

//基础路由 3
Route::get('test',function(){ return 'testx'; });

//多请求
Route::match(['get','post'],'xx/xx',function(){
    return 'heihei1';
});
//或者
Route::any('xx/xx',function(){
    return 'heihei2';
});

//路由传参
Route::get('user/{id}',function($id){
    return '用户的id是'.$id;
});

//两个参数
Route::get('user/{name}/{id}',function($name,$id){
    return '用户的名字是'.$name.'用户的id是'.$id;
});

//路由可选参数
Route::get('user/{name?}',function($name=null){ return '用户的名字是'.$name; });

//参数限制where(),用正则判断
Route::get('user/{name}',function($name){
    return '用户的名字是'.$name;
})->where('name','[a-zA-Z]+');

//多个参数限制
Route::get('user/{name}/{id}',function($name,$id){
    return '用户的名字是'.$name.'用户的id是'.$id;
})->where(['name'=>'[a-zA-Z]+','id'=>'\d+']);
//控制器路由,前一个参数随便填写,你开心就好
//例如admin/test或者test或者nikaixinjiuhao或者xx/xx/xxx/xxx/xx/xx,仍然可以访问
Route::get('xxx/xx','TestController@hello');
Route::get('xx/xx/xxx/xxx/xx/xx','TestController@hello');

 

//routes.php中
//控制器路由,前一个参数随便填写,你开心就好
//例如admin/test或者test或者nikaixinjiuhao或者xx/xx/xxx/xxx/xx/xx,仍然可以访问
Route::get('xxx/xx','Home\TestController@hello');
//直接写在模块外面
Route::get('xx/xx/xxx/xxx/xx/xx','Test2Controller@hello');

<?php
//控制器可以直接手动创建,或者使用cmd命令行创建
//TestController.php namespace App\Http\Controllers\Home; use App\Http\Controllers\Controller; class TestController extends Controller{ public function hello(){ echo 'hello world'; } }
<?php
//Test2Controller.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class Test2Controller extends Controller{
    public function hello(){
        echo 'hello world';
    }
}

 

 

赋值到模板:

 

TestController.php

<?php
//TestController.php
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;
class TestController extends Controller{
    public function hello(){
        return  'hello world';
    }

    public function phptemplate(){
        $data=['name'=>'zhangsan','userid'=>'39'];
        return view('test',$data);
    }

    public function phpblade(){
        $data=['name'=>'zhaowu','userid'=>'23'];
        return view('test2',$data);
    }
}

routes.php

Route::get('usertemplate','Home\TestController@phptemplate');
Route::get('userblade','Home\TestController@phpblade');

test页面

<!DOCTYPE html>
<html>
<head>
	<title>这是php形式的模板</title>
</head>
<body>
{{$name}}
{{$userid}}
<hr>
<span style="color:red;font-size:29px"><?php echo $name;?></span> <span style="color:red;font-size:39px"><?php echo $userid;?></span> </body> </html>

 test2.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>这是phpblade的模板</title>
</head>
<body>

<span style="color:red;font-size:29px"><?php echo $name;?></span>
<span style="color:red;font-size:39px"><?php echo $userid;?></span>
<hr>
<span style="color:red;font-size:29px">{{$name}}</span>
<span style="color:red;font-size:39px">{{$userid}}</span>
</body>

</html>

 得到效果,两者的区别一目了然:

 

posted @ 2016-12-06 18:05  岁月静好13543  阅读(294)  评论(0编辑  收藏  举报
AmazingCounters.com