我正在尝试将图像上传到Laravel应用程序,然后需要公开显示.但是,我似乎只能上传到应用程序根目录下的文件夹.任何上传到公共目录的尝试都会引发以下错误:
protected function getTargetFile($directory, $name = null)
{
if (!is_dir($directory)) {
if (false === @mkdir($directory, 0777, true)) {
throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
Run Code Online (Sandbox Code Playgroud)
我假设这意味着Laravel将阻止我上传到任何可公开访问的文件.我实际上更喜欢这样,但是,我如何链接到公共目录之外的图像我试图... /我的方式从公共文件夹,但适当地,这不起作用.
或者,任何让我直接上传到公共文件夹的内容也会很棒.
如果它有帮助,这是我的控制器方法将文件放在公共文件夹中:
$thumbnail_file = Input::file('thumbnail');
$destinationPath = '/uploads/'. str_random(8);
$thumbnail_filename = $thumbnail_file->getClientOriginalName();
$uploadSuccess = Input::file('thumbnail_url')->move($destinationPath, $thumbnail_filename);
Run Code Online (Sandbox Code Playgroud) 我经常发现自己希望能够让 trait 实现一个接口,这样当我将 trait 添加到类中时,我可以知道满足了 trait 的要求。例如,在 Laravel 中,我将为我的许多关系创建特征,如下所示:
trait HasOwner
{
public function owner()
{
return $this->belongsTo(User::class);
}
}
Run Code Online (Sandbox Code Playgroud)
在 Laravel 的情况下,我可以非常确定我添加它的每个模型都将有一个 ownsTo 方法,但是我仍然觉得我应该能够强制执行这个。
我知道强制执行此操作的唯一方法是使用 HasOwnerInterface 或 HasRelationshipsInterface 来支持它,但事实上,当我添加特性时未能添加它会防止它发出吱吱声,感觉就像在车上安装了安全气囊,但您需要这样做每次启动发动机时打开。
这是我认为将是完美的:
trait HasOwner expects RelationshipInterface
{
public function owner()
{
return $this->belongsTo(User::class);
}
}
interface RelationshipInterface
{
public function belongsTo(Model $model): Relationship;
}
class Property implements RelationshipInterface
{
use HasOwner;
}
Run Code Online (Sandbox Code Playgroud)
我应该为此使用另一种设计模式,还是我应该鼓起勇气,开始与 PHP 核心团队为此而战以添加它?
我的Laravel应用程序的每个响应似乎都会得到一个\n
字符前置,即使我返回的只是一个数字.
我觉得我必须把它作为一个错误引入,但我找不到我可能做过的任何地方.我看到我在Laravel中有一些其他的应用程序也有这个(但其他人没有)所以在拔掉我的头发后我想我会检查一下其他人是否经历过这样的事情.
问题是我正在尝试返回一个ID并使用它,但它具有这个主要特征.我可以剥离客户端,但宁愿修复root问题.
ETA:这条路线:
Route::get('/test', function(){
return 'hello world';
});
Run Code Online (Sandbox Code Playgroud)
产生这样的回应:
当然,这不是HTML的问题,但当我尝试将其指定为id时,我会得到"\n 234".