所以我有一个Laravel控制器:
class YeahMyController extends BaseController {
public function getSomething() {
Console::info('mymessage'); // <-- what do I put here?
return 'yeahoutputthistotheresponse';
}
}
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用artisan运行应用程序(它运行PHP的内置开发Web服务器):
php artisan serve
Run Code Online (Sandbox Code Playgroud)
我想将控制台消息记录到STDOUT
工匠流程的管道中.
我对laravel很新.我有一个基本问题,在laravel中添加常量的最佳方法是什么.我知道我们用来添加常量的.env方法.我还制作了一个常量文件,用于我的项目.例如:
define('OPTION_ATTACHMENT', 13);
define('OPTION_EMAIL', 14);
define('OPTION_MONETERY', 15);
define('OPTION_RATINGS', 16);
define('OPTION_TEXTAREA', 17);
Run Code Online (Sandbox Code Playgroud)
等等.它可以达到100或更多记录.那么编写常量的最佳方法应该是什么..env方法.或者添加constant.php文件?
谢谢
如果我想currentUser()
为一些oauth的东西做一个函数我正在做的我可以在视图或控制器中使用它(想想rails,你helper_method: current_user
在应用程序控制器中做的事情).
我读到的所有内容都声明要创建一个帮助文件夹并在那里添加函数然后你可以这样做Helpers::functionName
这是否是正确的方法呢?
什么是创建辅助功能的"laravel方式",可以在刀片模板和控制器中使用?
我正在尝试使用@yield和@section设置属性,但是如何?我试着用
<html @yield('mainApp')>
Run Code Online (Sandbox Code Playgroud)
和
@section('mainApp','id="myid"')
Run Code Online (Sandbox Code Playgroud)
但它返回id =" myid" 而不是id ="myid"
我知道我可以使用默认ID来管理它,但我不喜欢这种方式,如果我需要使用自定义属性呢?
我有一个自定义功能,我想在刀片模板中传递它.这是功能:
function trim_characters( $text, $length = 45, $append = '…' ) {
$length = (int) $length;
$text = trim( strip_tags( $text ) );
if ( strlen( $text ) > $length ) {
$text = substr( $text, 0, $length + 1 );
$words = preg_split( "/[\s]| /", $text, -1, PREG_SPLIT_NO_EMPTY );
preg_match( "/[\s]| /", $text, $lastchar, 0, $length );
if ( empty( $lastchar ) )
array_pop( $words );
$text = implode( ' ', $words ) . $append;
}
return $text;
}
Run Code Online (Sandbox Code Playgroud)
用法是这样的:
$string …
Run Code Online (Sandbox Code Playgroud) 默认的created_at日期保持打印为MySQL格式:2015-06-12 09:01:26 我想以自己的方式打印它12/2/2017
,以及将来的其他格式.
一个文件调用DataHelper.php
并存储在/app/Helpers/DateHelper.php
- 它看起来像这样
<?php
namespace App\Helpers;
class DateHelper {
public static function dateFormat1($date) {
if ($date) {
$dt = new DateTime($date);
return $dt->format("m/d/y"); // 10/27/2014
}
}
}
Run Code Online (Sandbox Code Playgroud)
能够在我的刀片视图中调用它
DateHelper::dateFormat1($user->created_at)
Run Code Online (Sandbox Code Playgroud)
我不知道下一步该做什么.
在php Laravel 5中创建自定义帮助函数的最佳实践是什么?
我有一些辅助课程app/Helpers
.如何使用a加载这些类service provider
以在刀片模板中使用它们?
例如,如果我有一个CustomHelper
包含方法的类fooBar()
:
<?php
nampespace App\Helpers;
class CustomHelper
{
static function fooBar()
{
return 'it works!';
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够在我的刀片模板中执行以下操作:
{{ fooBar() }}
Run Code Online (Sandbox Code Playgroud)
而不是这样做:
{{ \App\Helpers\CustomHelper::fooBar() }}
Run Code Online (Sandbox Code Playgroud)
PS: @安德鲁-布朗的回答中的最佳做法上Laravel 5个自定义的助手与非类文件的交易.拥有一个基于类的解决方案会很好,这样可以在类之间组织辅助函数.
我最近将laravel 4.2项目迁移到了5.0.到目前为止,我已经完成了所有必要的步骤,但我一直收到错误.
无法声明类Controller,因为该名称已在使用中
我的控制器已根据升级指南中的laravel提供的更改.
<?php
use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
abstract class Controller extends BaseController {
use DispatchesCommands, ValidatesRequests;
}
Run Code Online (Sandbox Code Playgroud)
我还将它添加到我的composer.json的classmap指令中.
"autoload": {
"classmap": [
"database",
"app/Http/Controllers"
],
"psr-4": {
"App\\": "app/"
}
},
Run Code Online (Sandbox Code Playgroud)
到目前为止我找不到任何解决方案,所以如果你们知道该怎么做它会帮助我很多:)提前感谢!
我有一个320个数组的数组,而常规var_dump
显示320个元素与所有嵌套元素,Laravel的dd
帮助器截断索引147处的嵌套元素,所有其他元素被截断,没有选项来展开它们,请参阅下面的示例
146 => array:17 [?
"total_unconfirmed_subscribers" => 0
"total_subscribers_subscribed_yesterday" => 0
"unique_list_id" => "24324"
"http_etag" => ""fbb6febfca8af5541541ea960aaedb""
"web_form_split_tests_collection_link" => "https://api.com/1.0/"
"subscribers_collection_link" => "https://api.com/1.0/"
"total_subscribers_subscribed_today" => 0
"id" => 23432
"total_subscribed_subscribers" => 0
"total_unsubscribed_subscribers" => 0
"campaigns_collection_link" => "https://api.com/1.0/"
"custom_fields_collection_link" => "https://api.com/1.0/accounts"
"self_link" => "https://api.com/1.0/accounts"
"total_subscribers" => 0
"resource_type_link" => "https://api.com/1.0/#list"
"web_forms_collection_link" => "https://api.com/"
"name" => "dccode"
]
147 => array:17 [?
"total_unconfirmed_subscribers" => 0
…16
]
148 => array:17 [ …17]
149 => array:17 …
Run Code Online (Sandbox Code Playgroud) laravel ×9
php ×7
laravel-5 ×5
composer-php ×2
laravel-5.4 ×2
blade ×1
console ×1
constants ×1
controllers ×1
debugging ×1
helpers ×1
laravel-5.3 ×1
logging ×1
view-helpers ×1