laravel5.2框架里,默认不带form模块,所以需要自己安装,参考:from模块安装
在路由里面添加新的路由app/Http/routes.php
Route::get('/articles','ArticlesController@index');Route::get('/articles/create','ArticlesController@create');//因为下面的{id}能够传入所有作为id的参数到show方法,所以需要先将create路由放在他的上面,这样在访问的时候,会优先匹配create路由,然后再到下面的Route::get('/articles/{id}','ArticlesController@show');Route::post('/articles/store','ArticlesController@store');//使用一个post路由,这个路由是为了处理create页面提交过来的表单的
在controller里面添加新的controllerapp/Http/Controllers/ArticlesController.php
public function create(){ // create方法,指向create.blade.php, return view('articles.create'); } public function store(Request $requests){ //store方法是接受create页面提交的数据的处理方法,这个Request $requests是注入实例,需要在页面use这个类Illuminate\Http\Request $input = $requests->all(); //获取request请求的所有数据 $input['publish_at']=Carbon::now(); Articles::create($input);//通过create方法写入数据库 return redirect('/articles'); //写入后重定向到首页 }
resources/views/articles/create.blade.php
@extends('layout.app')@section('content') {!! Form::open(array('url' => '/articles/store')) !!} //form表单提交的地址就是我们store路由 <!--- Title Field ---> <div class="form-group"> {!! Form::label('title', 'Title:') !!} {!! Form::text('title', null, ['class' => 'form-control']) !!} {!! Form::label('content','content:') !!} {!! Form::textarea('content',null,['class'=>'form-control']) !!} {!! Form::submit('submit',['class'=>'btn btn-primary form-control']) !!} </div>//这些都是laravelcollective html 和form模块的使用方法//{!! !!}是blade引擎的写法,主要是为了防止代码直接被解析为html,详情参考https://laravel.com/docs/5.2/blade {!! Form::close() !!}@stop
这是对应的html模板源码
< !DOCTYPE html><html><head> <title>Laravel</title> <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"/></head><body> <div class="container"> <form method="POST" action="http://localhost:8000/articles/store" accept-charset="UTF-8"><input name="_token" type="hidden"/> //action里面指向了我们的store路由,laravel自带了一个隐藏_token避免一些跨站提交 <!--- Title Field ---> <div class="form-group"> <label for="title">Title:</label> <input class="form-control" name="title" type="text" id="title"/> <label for="content">content:</label> <textarea class="form-control" name="content" cols="50" rows="10" id="content"></textarea> <input class="btn btn-primary form-control" type="submit" value="submit"/> </div> </form> </div> </body></html>
本文由 PeterYuan 创作,采用 署名-非商业性使用 2.5 中国大陆 进行许可。 转载、引用前需联系作者,并署名作者且注明文章出处。神一样的少年 »Laravel Forms 使用

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

Dependency injection provides object dependencies through external injection in PHP, improving the maintainability and flexibility of the code. Its implementation methods include: 1. Constructor injection, 2. Set value injection, 3. Interface injection. Using dependency injection can decouple, improve testability and flexibility, but attention should be paid to the possibility of increasing complexity and performance overhead.

Implementing dependency injection (DI) in PHP can be done by manual injection or using DI containers. 1) Manual injection passes dependencies through constructors, such as the UserService class injecting Logger. 2) Use DI containers to automatically manage dependencies, such as the Container class to manage Logger and UserService. Implementing DI can improve code flexibility and testability, but you need to pay attention to traps such as overinjection and service locator anti-mode.

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
