laravel7-路由

laravel7

环境准备

phpstudy_pro

​ 新版本的比旧版好用多了。我直接用新版本,下载安装非常简单,网上教程一大把。安装完成后再安装composer,window版的一路next下来就可以了,然后配置环境变量 如D:\phpstudy_pro\Extensions\php\php7.3.4nts

#更换国内阿里源 毕竟国外下载速度慢
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

laravel下载

##默认使用这种方法下载 laravel安装器下载可能有各种麻烦
D:\phpstudy_pro\WWW>composer create-project --prefer-dist laravel/laravel mylaravel
#部分结果
……
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAut
> @php artisan package:discover --ansi
Discovered Package: [32mfacade/ignition[39m
Discovered Package: [32mfideloper/proxy[39m
Discovered Package: [32mfruitcake/laravel-cors
Discovered Package: [32mlaravel/tinker[39m
Discovered Package: [32mnesbot/carbon[39m
Discovered Package: [32mnunomaduro/collision[3
[32mPackage manifest generated successfully.[3
43 packages you are using are looking for fundin
Use the `composer fund` command to find out more
> @php artisan key:generate --ansi
[32mApplication key set successfully.[39m

#这就下载完成了
#执行命令 然后打开一个网页看看 
D:\phpstudy_pro\WWW\mylaravel>php artisan serve
Laravel development server started: http://127.0.0.1:8000
[Sat Jun 27 16:22:49 2020] 127.0.0.1:52191 [200]: /favicon.ico
#在浏览器中输入 localhost:8000 就可以看到相应的首页了

正式篇

ROUTE

路由就是接受http请求的路径,并和程序交互的功能,也就是为访问url地址,所作的一些设置工作。

输入 php artisan serve命令 ,即可支持 localhost:8000内置的服务器

路由的定义文件在根目录下的rputes/web.php 中,可以看到welcom页面

路由提交

#直接访问 http://localhost:8000/index
Route::get('index', function () {
   
    return 'hello world';
});

::get()就是get提交

::post() ::put() ::delete是表单和ajax的提交方式

::any () 全部提交都可以只能提交响应

::match() 指定接受你的提交方式,用数组作为参数传递

Route::match(['get','post'],'match', function () {
   
    return 'hello laravel match';
});
#返回  hello laravel match

路由规则和闭包

在路由规则和闭包区域,可以设置传递路由参数

Route::get('/index/{id}', function ($id) {
   
    return 'hello laravel user id'.$id;
});
http://localhost:8000/index/5
#输出  hello laravel user id 5
#这里没有对id作限制所以也可以传递字符
http://localhost:8000/index/abc
#输出  hello laravel user id abc

控制器controller和路由进行匹配

php artisan make:controller TaskController
#在app/Http/Controller下生成TaskController
class TaskController extends Controller
{
   
    //
    public  function  index(){
   
        return 'task index';
    }
    public function read($id){
   
        return 'id:'.$id;
    }
}

#route/web.php
Route::get('/task', 'TaskController@index');
#http://localhost:8000/index
#输出  task index


Route::get('/task/read/{id}','TaskController@read');
#http://localhost:8000/read/10
#输出  id:10
#http://localhost:8000/read/abc
#输出  id:abc


#匹配数字 字符串出错
Route::get('/task/read/{id}','TaskController@read')->where('id','[0-9]+')#http://localhost:8000/read/10
#输出  id:10
#http://localhost:8000/task/read/abc
#出错

Route::get('/task/read/{id}','TaskController@read')->where('id','[0-9]+')->where(['id'=>'[0-9]+','name'=>'[a-z]+']);
#http://localhost:8000/read/10
#输出  id:10
#http://localhost:8000/task/read/10/abc
#出错

//必须两个参数
Route::get('/task/read/{id}/{name}','TaskController@read')->where(['id'=>'[0-9]+','name'=>'[a-z]+']);
#http://localhost:8000/read/1
#输出  id:11
#http://localhost:8000/task/read/11/abc
#输出 id:11 因为没有接收name


Route::get('/task/read/{id}','TaskController@read');
#多个匹配url直接在provides的 RouteServiceProvider -》boot方法 里面设置
 public function boot()
    {
   
        //这里可以对所有url的id参数做过滤
       Route::pattern('id','[0-9]+');
        parent::boot();
    }
    
#解除全局约束 也就是解除上面的Route::pattern('id','[0-9]+');的约束
Route::get('/task/read/{id}','TaskController@read')->where('id','.*');

#转跳
Route::get('/task', 'TaskController@index');
Route::redirect('index','task');
#http://localhost:8000/index 转跳到http://localhost:8000/task
#输出 task index

#还是转跳带状态码  301转跳
Route::redirect('/index','task',301);
Route::get('/task','TaskController@index');

#直接带状态码转跳  301转跳
Route::get('/task', 'TaskController@index');
Route::permanentRedirect('index','task');

视图路由

使用路由视图,要先创建一个视图V

视图路由有三个参数, uri(必) 名称(必) 参数(选)

#参数 URI http://localhost:8000/task
#参数 view resource/views/task.blade,php
#参数 传参 {
   {$id}}
Route::view('task','task',['id'=>'[0-9]+','name'=>'[a-z]+']);
http://localhost:8000/task
#404 因为还没有建立view文件 task.blade.php
#resources/views 下面建立task.blade.php文件
#输出  task 视图


Route::get
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值