Laravel学习第一天(创建laravel项目、路由、视图、blade模板)
创建laravel项目
composer create-project laravel/laravel learnlv 4.1.*
查看帮助:composer create-project
使用artisan工具
生成key:php artisan key:genrate,更多命令见:http://blog.luoyunshu.com/laravel-cheatsheet
路由
route.php:
<span><span>php</span></span>
<span>/*</span>
<span>|--------------------------------------------------------------------------</span>
<span>| Application Routes</span>
<span>|--------------------------------------------------------------------------</span>
<span>|</span>
<span>| Here is where you can register all of the routes for an application.</span>
<span>| It's a breeze. Simply tell Laravel the URIs it should respond to</span>
<span>| and give it the Closure to execute when that URI is requested.</span>
<span>|</span>
<span>*/</span>
<span>//向控制器传递参数,Route::get('/{id}')</span>
<span>//两种格式:1、Route::get('/', function(){})</span>
<span>// 2、Route::get('/', array('as'=>'home_route',function(){})) as的定义路由名称</span>
<span>Route<span>::<span>get<span>(<span>'/'<span>,<span> array<span>(<span>'as'<span>=><span>'home_route'<span>,<span> <span>function<span>()</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span>{</span>
<span> <span>//向视图传递参数</span></span>
<span> <span>//方法一:</span></span>
<span> <span>//$var = 'hello world';</span></span>
<span> <span>//return View::make('hello')->with('var', $var);</span></span>
<span> <span>//方法二</span></span>
<span> <span>//$var = 'abcd';</span></span>
<span> <span>//return View::make('hello', array('var'=>$var));</span></span>
<span> <span>//方法三</span></span>
<span> $var <span>=<span> <span>'def'<span>;</span></span></span></span></span>
<span> $view <span>=<span> <span>View<span>::<span>make<span>(<span>'index.hello'<span>);</span></span></span></span></span></span></span></span></span>
<span> $view<span>-><span>var<span> <span>=<span> $var<span>;</span></span></span></span></span></span></span>
<span> <span>return<span> $view<span>;</span></span></span></span>
<span>}));</span>
<span>//定义控制器</span>
<span>Route<span>::<span>get<span>(<span>'index'<span>,<span> <span>function<span>()</span></span></span></span></span></span></span></span></span>
<span>{</span>
<span> $arr <span>=<span> array<span>(</span></span></span></span>
<span> <span>'yunshu'<span>,</span></span></span>
<span> <span>'云舒'</span></span>
<span> <span>);</span></span>
<span> <span>return<span> <span>View<span>::<span>make<span>(<span>'index.index'<span>,<span> array<span>(<span>'arr'<span>=><span>$arr<span>));</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span>});</span>
<span>//生成路由URL与跳转</span>
<span>Route<span>::<span>get<span>(<span>'test'<span>,<span> <span>function<span>()</span></span></span></span></span></span></span></span></span>
<span>{</span>
<span> <span>//生成URL</span></span>
<span> $url <span>=<span> URL<span>::<span>route<span>(<span>'home_route'<span>);</span></span></span></span></span></span></span></span>
<span> <span>//echo $url;</span></span>
<span> <span>//跳转</span></span>
<span> <span>return<span> <span>Redirect<span>::<span>route<span>(<span>'home_route'<span>);</span></span></span></span></span></span></span></span></span>
<span>});</span>
blade布局
(master.blade.php):
<span>@include<span>(<span>'layout.header'<span>)</span></span></span></span>
<span></span>
<span> <span><div>
<p><code><span> <span><div>
<p><code><span> <span>@yield<span>(<span>'content'<span>)</span></span></span></span></span>
<span> <span><span>div<span>></span></span></span></span>
<span> <span><span>div<span>></span></span></span></span>
<span> <span><div>
<p><code><span> <span><div>
<p><code><span> <span>@section<span>(<span>'section'<span>)</span></span></span></span></span>
<span> <span>哈哈</span></span>
<span> <span>@show</span></span>
<span> <span><span>div<span>></span></span></span></span>
<span> <span><span>div<span>></span></span></span></span>
<span> <span>{{--<span> <span>注释代码--}}</span></span></span></span>
<span>@include<span>(<span>'layout.footer'<span>)</span></span></span></span>
index.blade.php:
<span>@extends<span>(<span>'layout.master'<span>)</span></span></span></span>
<span>{{--<span> <span>使用<span>master<span>模板<span> <span>--}}</span></span></span></span></span></span></span>
<span>{{--<span> <span>使用这部分内容填充模板<span> <span>--}}</span></span></span></span></span>
<span>@section<span>(<span>'content'<span>)</span></span></span></span>
<span> <span>@foreach<span>(<span>$arr <span>as<span> $a<span>)</span></span></span></span></span></span></span>
<span> <span>{{<span> $a <span>}}</span></span></span></span>
<span> <span>@endforeach</span></span>
<span> <span>{{--<span> <span>创建图片<span> <span>--}}</span></span></span></span></span></span>
<span> <span>{{<span> HTML<span>::<span>image<span>(<span>'image/1.jpg'<span>)<span> <span>}}</span></span></span></span></span></span></span></span></span></span>
<span>@stop</span>
<span>{{--<span> <span>覆盖或者重写父模板内容<span> <span>--}}</span></span></span></span></span>
<span>@section<span>(<span>'section'<span>)</span></span></span></span>
<span> <span>{{--<span> <span>拿到父模板的内容使用<span>@parent<span> <span>--}}</span></span></span></span></span></span></span>
<span> <span>@parent</span></span>
<span> <span>'你好呀'</span></span>
<span>@stop</span>
代码打包:
http://files.cnblogs.com/files/luoyunshu/learnlv.zip
以上就介绍了Laravel学习第一天(创建laravel项目、路由、视图、blade模板),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

使用ORM可简化PHP中的数据库操作,它将对象映射到关系数据库中。Laravel中的EloquentORM允许使用面向对象的语法与数据库交互,可通过定义模型类、使用Eloquent方法或在实战中构建博客系统等方式来使用ORM。

Laravel9和CodeIgniter4的最新版本提供了更新的特性和改进。Laravel9采用MVC架构,提供数据库迁移、身份验证和模板引擎等功能。CodeIgniter4采用HMVC架构,提供路由、ORM和缓存。在性能方面,Laravel9的基于服务提供者设计模式和CodeIgniter4的轻量级框架使其具有出色的性能。在实际应用中,Laravel9适用于需要灵活性和强大功能的复杂项目,而CodeIgniter4适用于快速开发和小型应用程序。

Laravel - Artisan 命令 - Laravel 5.7 提供了处理和测试新命令的新方法。它包括测试 artisan 命令的新功能,下面提到了演示?

比较Laravel和CodeIgniter的数据处理能力:ORM:Laravel使用EloquentORM,提供类对象关系映射,而CodeIgniter使用ActiveRecord,将数据库模型表示为PHP类的子类。查询构建器:Laravel具有灵活的链式查询API,而CodeIgniter的查询构建器更简单,基于数组。数据验证:Laravel提供了一个Validator类,支持自定义验证规则,而CodeIgniter的验证功能内置较少,需要手动编码自定义规则。实战案例:用户注册示例展示了Lar

对于初学者来说,CodeIgniter的学习曲线更平缓,功能较少,但涵盖了基本需求。Laravel提供了更广泛的功能集,但学习曲线稍陡。在性能方面,Laravel和CodeIgniter都表现出色。Laravel具有更广泛的文档和活跃的社区支持,而CodeIgniter更简单、轻量级,具有强大的安全功能。在建立博客应用程序的实战案例中,Laravel的EloquentORM简化了数据操作,而CodeIgniter需要更多的手动配置。

在选择大型项目框架时,Laravel和CodeIgniter各有优势。Laravel针对企业级应用程序而设计,提供模块化设计、依赖项注入和强大的功能集。CodeIgniter是一款轻量级框架,更适合小型到中型项目,强调速度和易用性。对于具有复杂需求和大量用户的大型项目,Laravel的强大功能和可扩展性更合适。而对于简单项目或资源有限的情况下,CodeIgniter的轻量级和快速开发能力则更为理想。

对于小型项目,Laravel适用于大型项目,需要强大的功能和安全性。CodeIgniter适用于非常小的项目,需要轻量级和易用性。

微服务架构使用PHP框架(如Symfony和Laravel)来实现微服务,并遵循RESTful原则和标准数据格式来设计API。微服务通过消息队列、HTTP请求或gRPC进行通信,并使用工具(如Prometheus和ELKStack)进行监控和故障排除。
