Home  >  Article  >  Backend Development  >  【laravel5.1-0.0.4】轻松实现Restful风格路由和控制

【laravel5.1-0.0.4】轻松实现Restful风格路由和控制

WBOY
WBOYOriginal
2016-06-20 12:46:38867browse

来自我的简书:http://www.jianshu.com/users/85c8826ce087/latest_articles
将以对文章的CURD操作作为示例,此篇只涉及基本的路由和控制器中> 方法,具体实现,将再后面继续讲解。

1.新建路由
    • 在app/Http/routes.php中添加:
      Route::resource('article','ArticleController');

    • 此resource路由中包含的子路由看后面的示例表;

    2. 控制器
    • 通过终端创建一个控制器
      php artisan make:controller ArticleController

    • 得到控制器类app/Http/Controllers/ArticleController

    • 如果要实现控制器文件在app/Http/Controllers下创建一个文件夹后,在里面创建控制器类,即可使用命令:
      php aritsan make:controller Article/ArticleController

    • 控制器类示例:

     3.(路由--->控制器)Restful对照关系示例表:   
    请求方法 路由地址 对应控制器方法 对应路由名称 当前作用
    GET /article index() route('article.index') 显示文章列表
    GET /article/ create() route('article.create') 文章新建页面
    POST /article store(Request $request) route('article.store') 文章存储操作
    GET /article/{id} show($id) route('article.index') 文章详情显示
    GET /article/{id}/edit edit($id) route('article.edit') 文章编辑页
    PUT/PATCH /article/{id}/ update(Request $request,$id) route('article.update') 文章更新操作
    DELETE /article/{id} destroy($id) route('article.destroy') 文章删除操作
    • 注:

      • 如果form表单要存储文章则 action ="route('article.store')"来对应URL

      • 访问 localhost:8000/article则访问的是 route('article.index')对应的URL

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn