• laravel路由和MVC


    传统的MVC中请求的一般是控制器,laravel中请求的是路由
        
        laravel中的路由简单的说就是将用户的请求转发给相应的程序进行处理,作用就是建立url和程序之间的映射
    
        路由请求类型get、post、put、patch、delete
    
        基本路由:
            路由文件  app/Http/routes.php
    
            get方式:
                Route::get('basic1', function () {
                    return 'basic1';
                });
                测试地址 http://www.laravelstudy.com/laravel/public/basic2
                测试结果 basic1
    
            post方式:
                Route::post('basic2', function () {
                    return 'basic2';
                });
    
                测试地址 post不能通过url访问
    
            match方式:
                Route::match(['get', 'post'], 'multy1', function () {
                    return 'multy1';
                });
                测试地址 http://www.laravelstudy.com/laravel/public/multy1
                测试结果 multy1
    
            any方式:
                Route::any('multy2', function () {
                    return 'multy2';
                });
                测试地址 http://www.laravelstudy.com/laravel/public/multy2
                测试结果 multy2
        
        路由参数:
            Route::get('user/{id}', function ($id) {
                return $id;
            });
            测试地址 http://www.laravelstudy.com/laravel/public/user/3
            测试结果 3
    
            Route::get('user/{name?}', function ($name = 'default') {
                return $name;
            });
            测试地址 http://www.laravelstudy.com/laravel/public/user
            测试结果 default
    
            测试地址 http://www.laravelstudy.com/laravel/public/user/xiaodong
            测试结果 xiaodong
    
            还可以使用正则匹配
            Route::get('user/{name?}', function ($name = 'default') {
                return $name;
            })->where('name', '[A-Za-z]+');
    
        路由别名:
            给当前的路由设置别名,不管url如何变化,route函数都能正确获取到url
            Route::get('user/gerenzhongxin', ['as' => 'center', function () {
                return route('center');
            }]);
            测试地址 http://www.laravelstudy.com/laravel/public/user/gerenzhongxin
            测试结果 http://www.laravelstudy.com/laravel/public/user/gerenzhongxin
    
        路由群组:
            给当前的路由设置前缀
            Route::group(['prefix' => 'member'], function () {
                Route::get('name', function () {
                    return 'name';
                });
    
                Route::any('age', function () {
                    return 'age';
                });
            });    
            测试地址 http://www.laravelstudy.com/laravel/public/member/name
            测试结果 name
    
            测试地址 http://www.laravelstudy.com/laravel/public/member/age
            测试结果 age
    
        路由输出视图:
            view方法用于输出视图文件
            Route::get('index', function () {
                return view('index');
            });
    
        通过a链接传递参数:
            <a href="{{ URL('participle/aid/12') }}">
    
            Route::any('/participle/aid/{id}', 'ParticipleController@select');
    
            public function select($id){
                echo $id;
            }
  • 相关阅读:
    用Xamarin + VS 编写Android程序体验及其与Android Studio的比较
    【Android】XML文件的解析
    【Ubuntu】您没有查看“sf_VirtualDisk”的内容所需的权限。
    Android酷炫实用的开源框架(UI框架)
    Linux下安装gcc 、g++ 、gfortran编译器
    Ubuntu 分辨率调整及操作问题解决
    “this kernel requires an x86-64 CPU, but only detects an i686 CPU, unable to boot” 问题解决
    【Android】沉浸式状态栏实现
    【Android】基于TCP协议的网络通信
    C#中string和byte[]相互转换问题解决
  • 原文地址:https://www.cnblogs.com/liuxiaowei/p/7163495.html
Copyright © 2020-2023  润新知