You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
单一职责原则
一个类和一个方法应该只有一个责任。
例如:
更优的写法:
保持控制器的简洁
如果您使用的是查询生成器或原始SQL查询,请将所有与数据库相关的逻辑放入Eloquent模型或Repository类中。
例如:
更优的写法:
使用自定义Request类来进行验证
把验证规则放到 Request 类中.
例子:
更优的写法:
业务代码要放到服务层中
控制器必须遵循单一职责原则,因此最好将业务代码从控制器移动到服务层中。
例子:
更优的写法:
DRY原则 不要重复自己
尽可能重用代码,SRP可以帮助您避免重复造轮子。 此外尽量重复使用Blade模板,使用Eloquent的 scopes 方法来实现代码。
例子:
更优的写法:
使用ORM而不是纯sql语句,使用集合而不是数组
使用Eloquent可以帮您编写可读和可维护的代码。 此外Eloquent还有非常优雅的内置工具,如软删除,事件,范围等。
例子:
更优的写法:
集中处理数据
例子:
更优的写法:
不要在模板中查询,尽量使用惰性加载
例子 (对于100个用户,将执行101次DB查询):
更优的写法 (对于100个用户,使用以下写法只需执行2次DB查询):
注释你的代码,但是更优雅的做法是使用描述性的语言来编写你的代码
例子:
加上注释:
更优的写法:
不要把 JS 和 CSS 放到 Blade 模板中,也不要把任何 HTML 代码放到 PHP 代码里
例子:
更好的写法:
在Javascript文件中加上:
当然最好的办法还是使用专业的PHP的JS包传输数据。
在代码中使用配置、语言包和常量,而不是使用硬编码
例子:
更优的写法:
使用社区认可的标准Laravel工具
强力推荐使用内置的Laravel功能和扩展包,而不是使用第三方的扩展包和工具。
如果你的项目被其他开发人员接手了,他们将不得不重新学习这些第三方工具的使用教程。
此外,当您使用第三方扩展包或工具时,你很难从Laravel社区获得什么帮助。 不要让你的客户为额外的问题付钱。
遵循laravel命名约定
来源 PSR standards.
另外,遵循Laravel社区认可的命名约定:
ArticlesControllerarticle/1users.show-active, show-active-usersUsersarticleComments, article_commentarticleComment, article_commentsarticle_comment, articleCommentsuser_article, articles_usersMetaTitle; article_meta_title$model->createdAtArticleId, id_article, articles_idcustom_id2017_01_01_000000_articlesget_allsaveArticletest_guest_cannot_see_article$articles_with_author$active, $data$users, $objArticlesEnabled; articles-enabledshowFiltered.blade.php, show_filtered.blade.phpgoogleCalendar.php, google-calendar.phpAuthenticatable, IAuthenticationNotificationTraitNotificationUserTypes,UserTypeEnumUpdateUserFormRequest,UserFormRequest,UserRequestUsersSeeder尽可能使用简短且可读性更好的语法
例子:
更优的写法:
更多示例:
Session::get('cart')session('cart')$request->session()->get('cart')session('cart')Session::put('cart', $data)session(['cart' => $data])$request->input('name'), Request::get('name')$request->name, request('name')return Redirect::back()return back()is_null($object->relation) ? null : $object->relation->idoptional($object->relation)->idreturn view('index')->with('title', $title)->with('client', $client)return view('index', compact('title', 'client'))$request->has('value') ? $request->value : 'default';$request->get('value', 'default')Carbon::now(), Carbon::today()now(), today()App::make('Class')app('Class')->where('column', '=', 1)->where('column', 1)->orderBy('created_at', 'desc')->latest()->orderBy('age', 'desc')->latest('age')->orderBy('created_at', 'asc')->oldest()->select('id', 'name')->get()->get(['id', 'name'])->first()->name->value('name')使用IOC容器来创建实例 而不是直接new一个实例
创建新的类会让类之间的更加耦合,使得测试越发复杂。请改用IoC容器或注入来实现。
例子:
更优的写法:
避免直接从
.env文件里获取数据将数据传递给配置文件,然后使用
config()帮助函数来调用数据例子:
更优的写法:
使用标准格式来存储日期,用访问器和修改器来修改日期格式
例子:
{{ Carbon::createFromFormat('Y-d-m H-i', $object->ordered_at)->toDateString() }} {{ Carbon::createFromFormat('Y-d-m H-i', $object->ordered_at)->format('m-d') }}更优的写法:
其他的一些好建议
永远不要在路由文件中放任何的逻辑代码。
尽量不要在Blade模板中写原始 PHP 代码。
Beta Was this translation helpful? Give feedback.
All reactions