2015-02-23 132 views
0

我想知道如果有人知道我有這個奇怪的錯誤,我有一個簡單的窗體上有一個文件輸入字段。當你上傳圖片時,它會將圖片添加到表單對象中,並將其發佈到該帖子中,以便我可以檢索它,但這不像我想象的那樣工作。該指數方法顯示與圖像的帖子:爲什麼我在laravel網站的圖片上總是收到404錯誤?

enter image description here

這裏是create.blade.php:

{!! Form::model(new Boroughcc\Post, ['route' => ['posts.store'], 'files' => true]) !!} 
     @include('posts/partials/_form', ['submit_text' => 'Create Post']) 
    {!! Form::close() !!} 

這裏是部分:

<div class="form-group"> 
    {!! Form::label('title', 'Title:') !!} 
    {!! Form::text('title') !!} 
</div> 
<div class="form-group"> 
    {!! Form::label('featured_image', 'Featured Image:') !!} 
    {!! Form::file('featured_image') !!} 
</div> 
<div class="form-group"> 
    {!! Form::label('body', 'Post body:') !!} 
    {!! Form::textarea('body', null, array('id'=>'','class'=>'sir-trevor')) !!} 
</div> 
<div class="form-group"> 
    {!! Form::submit($submit_text, ['class'=>'btn primary']) !!} 
</div> 

這是所有似乎都很好,但它只是沒有將圖像添加到帖子本身的顯示部分,實質上它沒有找到圖像,因爲我在實際的圖像上獲得了404錯誤st頁面本身。

enter image description here

這裏是我加入了圖像的post.show觀點:

@extends('app') 

@section('content') 
<section style="clear: both;"> 
    <article class="letterbox" style="background-image:url({{ $post->featured_image }});"></article> 
</section> 
<section id="product-intro-name"> 
    <hgroup class="article-header"> 
     <h1>{!! $post->slug_column !!}</h1> 
    </hgroup> 
</section> 
<article class="container" id="{!! $post->slug !!}"> 

    <section> 
     <article class="post col-lg-6 col-md-6 col-sm-6"> 
      <img src="{!! $post->featured_image !!}" alt="{!! $post->slug !!} image" /> 
      {!! $post->body !!} 
     </article> 
    </section> 

</article> 
@if (Auth::guest()) 
@else 
    {!! link_to_route('posts.edit', 'Edit the post', array($post->getSlug()), array('class' => 'btn btn-info')) !!} 
@endif 

@stop 

這是我在全PostsController:

<?php namespace Boroughcc\Http\Controllers; 

use Input; 
use Redirect; 
use Storage; 
use SirTrevorJs; 
use STConverter; 
use Validator; 
use Image; 
use Boroughcc\Post; 
use Boroughcc\Http\Controllers\Controller; 
use Request; 

class PostsController extends Controller { 

    // public function __construct() 
    // { 
    //  $this->addTitleSegments(['Journal', 'Post']); 
    // } 


    /** 
    * Display a listing of the resource. 
    * 
    * @return Response 
    */ 
    public function index() 
    { 
     $posts = Post::all(); 
     return view('posts.index', compact('posts')); 
    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return Response 
    */ 
    public function create() 
    { 
     // 
     $this->middleware('auth'); 
     return view('posts.create'); 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @return Response 
    */ 
    public function store() 
    { 
     // image upload function 

     $input = Input::all(); 
     $validation = Validator::make($input, Post::$rules); 

     // Get the uploaded file object 
     $img = Input::file('featured_image'); 

     // Generate the necessary file details 
     $extension = pathinfo($img->getClientOriginalName(), PATHINFO_EXTENSION); 
     $filename = date('Y-m-d-H:i:s') . '.' . $extension; 
     $path = 'img/posts/'; 

     // Move the uploaded image to the specified path 
     // using the generated specified filename 
     $img->move($path, $filename); 

     // Save the post to the database 
     // using the path and filename use above 
     //dd($input); 
     $post = Post::create(array(
      'title' => Input::get('title'), 
      'body' => Input::get('body'), 
      'featured_image' => $path . $filename 
     )); 

     return Redirect::route('posts.index')->with('message', 'Post created'); 


     //Post::create($input); 

     // return Redirect::route('posts.index')->with('message', 'Post created'); 


    } 

    /** 
    * Display the specified resource. 
    * 
    * @param int $id 
    * @param int $slug_column 
    * @return Response 
    */ 
    public function show(Post $post) 
    { 
     // 
     $post->find($post->slug); 
     $img = Input::get('featured_image'); 

     if ($post->slug !== null) { 
      return Redirect::route('posts.show', array('id' => $post->id, 'slug_column' => $post->slug), 301); 
     } 
     return view('posts.show', compact('post')); 
    } 

    /** 
    * Show the form for editing the specified resource. 
    * 
    * @param int $id 
    * @return Response 
    */ 
    public function edit(Post $post) 
    { 
     // 
     $post->find($post->slug); 
     $this->middleware('auth'); 
     return view('posts.edit', compact('post')); 
    } 

    /** 
    * Update the specified resource in storage. 
    * 
    * @param int $id 
    * @return Response 
    */ 
    public function update(Post $post) 
    { 
     //$post = $post->find($post->slug); 
     $this->middleware('auth'); 
     // $input = array_except(Input::all(), '_method'); 
     //$input = Request::all(); 
     $input = Request::except('_method','_token'); 
     $post->update($input); 

     return Redirect::route('posts.show', $post->getSlug())->with('message', 'Post updated.'); 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param int $id 
    * @return Response 
    */ 
    public function destroy(Post $post) 
    { 
     // 
     $post->delete(); 
     return Redirect::route('posts.index')->with('message', 'Post deleted.'); 
     //dd($post->toArray()); 

    } 

} 

我可以在這裏創建一個職位並更新帖子,但出於某種原因,添加到帖子的圖片根本不顯示。

我的路線文件是罰款,它的第一件事,我以爲會是問題,除非是:

// Route for upload images 
Route::any("/sirtrevorjs/upload", array("uses" => "[email protected]")); 

// Route for tweets 
Route::any("/sirtrevorjs/tweet", array("uses" => "[email protected]")); 

/// Posts //// 

Route::model('posts', 'Post'); 

Route::resource('posts', 'PostsController'); 

Route::match(array('PATCH'), "/posts", array(
     'uses' => '[email protected]', 
     'as' => 'posts.update' 
)); 

Route::bind('posts', function($value, $route) { 
    return Post::whereId($value)->first(); 
}); 

Route::bind('posts', function($value, $route) { 
    return Post::whereSlugColumn($value)->first(); 
}); 

Route::get('/', '[email protected]'); 

Route::get('/about', ['uses' => '[email protected]', 
    'as' => 'about'] 
); 

Route::get('/journal', ['uses' => '[email protected]', 
    'as' => 'journal'] 
); 

Route::controllers([ 
    'auth' => 'Auth\AuthController', 
    'password' => 'Auth\PasswordController', 
]); 

我知道我會踢自己的,但我無法弄清楚。

+0

,你能否告訴以下之一: *圖像源(點擊右鍵,查看源文件和複製生成的img標籤) *的開發人員工具錯誤控制檯 – 2015-02-23 19:54:52

+0

'無法加載資源:服務器響應時的狀態爲404(Not Found)http:// localhost:8000/posts/img/posts/2015-02-23-19:26:35.gif'當我看着它時,圖像在那裏,當我在其中鍵入該網址顯示圖像 – 2015-02-23 19:59:51

+0

我知道發生了什麼,出於某種原因在節目中查看/ posts url內部並添加了'/ posts/img/2015-02-23-19:26:35 .gif'這反過來給'/ posts/img/posts/2015-02-23-19:26:35.gif'這是不正確的'/ posts/img/2015-02-23-19:26: 35.gif' – 2015-02-23 20:07:17

回答

0

嘗試用斜槓/
<img src="{!! $post->featured_image !!}" alt="{!! $post->slug !!} image" /><img src="/{!! $post->featured_image !!}" alt="{!! $post->slug !!} image" />

前面加上圖像源通過使用絕對路徑,你要確保你的鏈接將永遠在你的項目的根「解決」,沒有你如果你在/posts/之內,不用擔心。

0

只需添加一個/到您的圖片src路徑,以便它是從你的應用程序的根拉到相對,而不是當前的URI

<section> 
    <article class="post col-lg-6 col-md-6 col-sm-6"> 
     <img src="/{!! $post->featured_image !!}" alt="{!! $post->slug !!} image" /> 
     {!! $post->body !!} 
    </article> 
</section> 

如果沒有/瀏覽器會在前面加上當前URI到例如,如果您位於網址爲http://localhost.com/foo的網頁上,您的圖片來源將變爲http://localhost.com/foo/path/to/my/image.png。通過添加/你告訴該圖像是位於關你的站點的根目錄瀏覽器http://localhost.com/path/to/my/image.png

+0

爲什麼downvote? – 2015-02-23 21:34:07

0

我用下面的語句

<img src="{{{ .$post->featured_image }}}" alt="{!! $post->slug !!} image" /> 

和我的形象在公共/圖片/文件夾 後來我$ post-> featured_image將返回/ images/imagename。PNG

這對我的作品

+0

確保您將所有圖像文件放入公有資源中 – SharkIng 2015-02-23 20:26:37

相關問題