手把手教你跑Larave框架实战笔记系列之二

作者: 何朱必 分类: 开发体验笔记 发布时间: 2018-03-16 21:58 ė views 6 28条评论

 

系列之一为童鞋们搭好了舞台,今天正式登台跑框架了……

从“路由”开始,玩一把“Hellow world!”

Route::get(‘/’, function () {

//return view(‘welcome’);

return ‘Hellow world!’;

});

laravel 的每一个路由是需要手动定义的,默认欢迎页面(参数)

是 resources/views 目录下的文件名welcome.blade.php去掉 blade.php后缀,对应的路由

url 是 http://www.rk.com/

依此类推,把根目录“/”换成子目录“tests”模拟欢迎页面新定义一个路由:

Route::get(‘tests’, function () {

return view(‘logo’);

});

调用页面(参数)

是 resources/views 目录下的文件名logo.blade.php去掉 blade.php后缀

多多找一下路由(R)与(V)示例文件模仿书写格式,依此类推,只要格式相互对应起来就可以发生关联,调用视图

url 就应该是 http://www.rk.com/tests

就是这样子,把MVC中的路由和视图(V)关联起来了,万里长征开始了第一步,以后编程的日子里还会随时这样玩……

以上例子说明了路由可以分发请求,这就好比家里的路由器可以分发多路请求,满足手机、电视……可以有线连接,也可以wifi
路由中还可以引入 html 页面,可以在 route/web.php 中搞定一切。
但是如果把业务逻辑都写入到路由中,那路由就变成了原生开发“搭积木”,web.php页面代码庞大难以维护。
接下来控制器就登台表演了……
把业务逻辑写在控制器中,路由只负责转发请求到指定的控制器即可。

先前,我们已经在PhpStorm中安装了昵称为artisan 的 laravel 命令行接口,也就是根目录下的 artisan 文件,用法是输入命令:

Php artisan

意思就是使用 php 的命令行模式运行 artisan.php 文件,php命令行下是可以不需要文件后缀就能识别的,所以根目录下的 artisan 文件并没有带上 .php

Laravel构架之所以被赞誉为“为Web艺术家而生”,其优雅、艺术、现代主要就是表现在“用少量的代码来干很多漂亮活……”

php artisan.php 命令行主要有两个作用:
第一是生成文件,
第二是执行任务。
现在用它生成控制器(C)文件:

〖法一〗如果不带任何参数用它来生成普通控制器模板的话,输入命令:

Php artisan make:controller ArticleController

见证奇迹的时候又要到来了……
结果是生成了一个 app/Http/Controllers/ArticleController.php 文件;
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticleController extends Controller

{

//

}

不但自动创建了文件,而且还定义好了命名空间,继承好了父级控制器,在这个模板上直接写管理MySQL的增、删、改、查的方法就可以了;

〖法二〗不想一来就自定义语句或方法,想用artisan先生成通用方法代码,再根据需要少量修改代码,也就是说用有点高逼格的方式来干写代码苦力活,那么只需加个–resource选项参数:

Php artisan make:controller ArticleController--resource

不但增、删、改、查的方法都定义好了,连注释都给写好了。这其实是按 RESTful 规范生成的格式,除了 GET POST 它还包含了一大堆请求方式:
PUT 、 PATCH 、 DELETE 、HEAD 、OPTIONS
也就是说 Route:: 后面还可以跟上面这些方法。

视图如何用?除了在路由中直接调用外,就是可以在程序中用函数来调用:例如调用视图admin/index.blade.php就在程序中用这个方法

public function index() {

return view(‘admin/index’);

}

就行了,视图使用容易理解。那么,控制器如何用?当然是放在路由中直接替代闭包函数来使用。因为路由(R)除了请求调用视图(V)外,更多的是请求来干太多的事,比方说处理数据模型(M)等等,如果把干这些活的代码都放这个闭包函数中是臃肿且难以维护的,所以才引入控制器这个中间件。很显然,控制器就是来替代路由中这第二个参数的闭包函数的。路由(R)-控制器(C)-视图(V)就这样轻松关联起来了,三者玩起来思路也很清晰,不难理解。
路由(R)-控制器(C)用起来也非常简单,直接写控制器名在路由(R)闭包函数的这个位置即可,然后用 @ 符号分割控制器和控制器的方法(可以是形参或闭包函数),例如:

Route::get(‘article/index’, ‘ArticleController@index’)

Route::post(‘article/store’, ‘ArticleController@store’)

当控制器(C)用处理数据模型时,就变成了:路由(R)-控制器(C)-数据模型(M)-视图(V)也关联起来了,四者不是同时一起用,就可能有多种排列组合,处理多种事务,响应多种请求。

本文出自何朱必博客,转载时请注明出处及相应链接。

本文永久链接: http://www.hezhubi.com/larave2.htm

0

28条评论

  1. Deon 2019年7月23日 上午6:55 回复

    Oh my goodness! Impressive article dude! Thank you so much, However I am having problems with
    your RSS. I don’t understand why I cannot subscribe to it.
    Is there anybody getting identical RSS issues?
    Anyone who knows the answer can you kindly respond? Thanx!!

  2. Victor 2019年7月23日 上午6:45 回复

    I’ve been browsing online greater than three hours lately,
    but I never found any interesting article like yours.
    It is beautiful price enough for me. Personally,
    if all site owners and bloggers made good content material as you did, the web will be a lot more useful than ever
    before.

  3. Grady 2019年7月22日 上午11:46 回复

    Magnificent goods from you, man. I’ve understand your stuff previous to and you
    are just too magnificent. I actually like what you’ve acquired here, really like what you are stating and the way in which you say it.
    You make it enjoyable and you still take care of to keep it
    sensible. I can’t wait to read much more from you. This is really a terrific website.

  4. Louie 2019年7月22日 上午2:22 回复

    If some one desires to be updated with hottest technologies therefore he must be visit this
    site and be up to date every day.

  5. Adele 2019年7月21日 上午2:29 回复

    Hello There. I found your blog using msn. This is
    a very well written article. I’ll make sure to bookmark it and come back to read more of your useful information. Thanks for
    the post. I will certainly return.

  6. Margot 2019年7月21日 上午12:49 回复

    Pretty! This has been an incredibly wonderful post. Many thanks for
    supplying these details.

  7. Rebekah 2019年7月20日 下午10:22 回复

    This is really interesting, You are a very skilled blogger.

    I’ve joined your feed and look forward to seeking more of your wonderful post.
    Also, I have shared your web site in my social networks!

  8. Roxanne 2019年7月20日 下午2:29 回复

    Wow, that’s what I was searching for, what a stuff!

    present here at this webpage, thanks admin of this web page.

  9. Dawn 2019年7月19日 下午9:57 回复

    I think the admin of this website is actually working hard in support of his site,
    since here every information is quality based information.

  10. Leigh 2019年7月19日 下午8:13 回复

    Hey there! This is kind of off topic but I need
    some help from an established blog. Is it difficult to
    set up your own blog? I’m not very techincal but I can figure things out
    pretty quick. I’m thinking about making my own but I’m not sure
    where to start. Do you have any tips or suggestions?
    With thanks

  11. Hortense 2019年7月19日 上午5:32 回复

    Hey! I just wanted to ask if you ever have any problems with
    hackers? My last blog (wordpress) was hacked and
    I ended up losing months of hard work due to no data backup.
    Do you have any solutions to prevent hackers?

  12. Sunny 2019年7月18日 上午9:35 回复

    I think everything said made a ton of sense.
    However, what about this? suppose you wrote a catchier title?
    I am not suggesting your content isn’t good., however suppose you added something that grabbed a person’s
    attention? I mean 手把手教你跑Larave框架实战笔记系列之二 is a little plain.
    You could look at Yahoo’s home page and see how they write news titles to grab people
    to click. You might add a video or a pic or two to grab people interested about everything’ve got to say.
    In my opinion, it could make your posts a little livelier.

  13. Chastity 2019年7月16日 下午10:58 回复

    you are in point of fact a good webmaster. The site loading speed is amazing.
    It sort of feels that you’re doing any unique trick.
    In addition, The contents are masterwork. you have performed a wonderful process on this matter!

  14. Clement 2019年7月16日 下午2:11 回复

    Greetings from Ohio! I’m bored to tears at work so I decided to
    check out your blog on my iphone during lunch break. I enjoy the info you provide
    here and can’t wait to take a look when I get home.
    I’m shocked at how quick your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyhow, excellent blog!

  15. Dallas 2019年7月16日 上午3:20 回复

    Now I am ready to do my breakfast, afterward having my breakfast coming again to read additional news.

  16. Edith 2019年7月13日 下午1:34 回复

    Great site. A lot of useful information here. I am sending it to several friends ans additionally sharing in delicious.
    And obviously, thanks to your sweat!

  17. Alisia 2019年7月13日 上午3:39 回复

    Excellent, what a blog it is! This webpage provides helpful information to us, keep it up.

  18. Teresa 2019年7月13日 上午2:29 回复

    I’m amazed, I have to admit. Seldom do I encounter a blog that’s both
    equally educative and entertaining, and without a doubt, you’ve hit
    the nail on the head. The issue is something which not enough folks are speaking
    intelligently about. Now i’m very happy that I stumbled across this during my search for
    something relating to this.

  19. Reina 2019年7月12日 下午7:13 回复

    I need to to thank you for this wonderful read!!

    I definitely loved every bit of it. I’ve got you book marked to look at
    new stuff you post…

  20. Lela 2019年7月12日 下午12:12 回复

    Heya i am for the first time here. I found this board and I to
    find It truly useful & it helped me out a lot.
    I hope to offer something back and aid others like you helped me.

  21. Rufus 2019年7月12日 上午5:12 回复

    This design is incredible! You certainly know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog
    (well, almost…HaHa!) Wonderful job. I really loved what you had to say,
    and more than that, how you presented it. Too
    cool!

  22. Eartha 2019年7月12日 上午1:14 回复

    Useful info. Lucky me I found your site by chance,
    and I am stunned why this coincidence didn’t came about in advance!

    I bookmarked it.

  23. Carroll 2019年7月11日 下午12:50 回复

    It’s a pity you don’t have a donate button! I’d without a doubt donate to this brilliant
    blog! I guess for now i’ll settle for bookmarking and adding your RSS feed
    to my Google account. I look forward to brand new updates and will talk about
    this blog with my Facebook group. Talk soon!

  24. Stewart 2019年7月11日 上午8:46 回复

    Heya i am for the first time here. I came across this board
    and I find It truly useful & it helped me out
    a lot. I hope to give something back and help
    others like you helped me.

  25. Elvin 2019年7月11日 上午4:01 回复

    I’m extremely inspired with your writing talents as neatly as with the structure on your weblog.
    Is this a paid theme or did you customize it yourself?

    Anyway stay up the excellent high quality writing,
    it is uncommon to look a nice weblog like this one these days..

  26. Alva 2019年7月11日 上午2:33 回复

    Excellent article. Keep writing such kind of information on your page.
    Im really impressed by your site.
    Hi there, You have done a great job. I’ll definitely
    digg it and for my part recommend to my friends.

    I am sure they will be benefited from this website.

  27. Elvira 2019年7月10日 下午5:55 回复

    I would like to thank you for the efforts you’ve put in penning this blog.
    I really hope to check out the same high-grade blog posts
    from you in the future as well. In truth, your creative writing abilities has motivated
    me to get my own site now 😉

  28. bit.ly 2019年6月29日 上午7:04 回复

    For latest information you have to pay a quick visit world-wide-web and on the web I found this web page as
    a finest web page for latest updates.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Ɣ回顶部