zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    CentOS操作记录
    CentOS 6.4 服务器版安装教程(超级详细图解)
    一个过滤特殊字符的JS
    PowerDesigner 15设置mysql主键自动增长及基数
    使用PowerDesigner设计建造MySQL数据库
    PowerDesigner15在win7-64位系统下对MySQL 进行反向工程以及建立物理模型产生SQL语句步骤图文傻瓜式详解
    完全卸载mysql步骤
    FilterDispatcher已被标注为过时解决办法 &gt;&gt;&gt; FilterDispatcher &lt;&lt;&lt; is deprecated!
    Server Tomcat v7.0 Server at localhost was unable to&amp;nbs 报错问题解决
    eclipse js 报错解决办法
  • 原文地址:https://www.cnblogs.com/liuxiaowei/p/7163495.html
Copyright © 2011-2022 走看看