2013-07-30 98 views
1

我已经看到使用专用路由的Redactor imageUpload的几个解决方案,但是我选择使用资源控制器来执行所有路由。Redactor imageUpload和Laravel 4资源控制器

的标记:

<div class="row"> 
    <div class="large-12 columns"> 
     {{ Form::model($page, array('method' => 'put', 'route' => array('pages.update', $page->id), 'files' => true)) }} 

     <div class="row"> 
      <div class="large-12 columns"> 
       {{ Form::label('title', 'Title') }} 
       {{ Form::text('title') }} 
      </div> 
     </div> 

     <div class="row"> 
      <div class="large-12 columns"> 
       {{ Form::label('body', 'Content') }} 
       {{ Form::textarea('body') }} 
      </div> 
     </div> 

     <div class="row"> 
      <div class="large-12 columns"> 
      {{ Form::submit('Save', array('class' => 'button')) }} 
      <a href="{{ URL::route('pages.index') }}" class="btn btn-large">Cancel</a> 
     </div> 

    {{ Form::close() }} 
    </div> 
</div> 

<script> 
$(document).ready(function() 
{ 
    $(this).foundation(); 

    $('#body').redactor({ 

    iframe: true, 
    css: "{{ url('sleddog-assets/stylesheets/app.css') }}", 
    imageUpload: 'pages.edit' 
    }); 

}); 
</script> 

代码:

class PagesController extends \BaseController { 

/** 
* Display a listing of the resource. 
* 
* @return Response 
*/ 
public function index() 
{ 
    //$pages = Page::all(); 
    //return $pages; 
    return \View::make('admin.pages.pages')->with('pages', Page::all()); 
} 

/** 
* Show the form for creating a new resource. 
* 
* @return Response 
*/ 
public function create() 
{ 
    return \View::make('admin.pages.create'); 
    //return "Create"; 
} 

/** 
* Store a newly created resource in storage. 
* 
* @return Response 
*/ 
public function store() 
{ 
    $validation = new PageValidator; 

    if ($validation->passes()) { 
     // $file = Input::file('file'); 
     // if(Input::upload('file', 'public/images', $file['name'])) { 
     // $image = Response::json(array('filelink' => 'images/' . $file['name'])); 
     // } 

     $page = new Page; 
     $page->title = Input::get('title'); 
     $page->slug = Str::get('slug'); 
     $page->body = Input::get('body'); 
     $page->user_id = Sentry::getUser()->id; 


     $page->save(); 

     return Redirect::route('admin.pages.edit'); 
    } 

    return Redirect::back()->withInput()->withErrors($validation->errors); 
} 

/** 
* Display the specified resource. 
* 
* @param int $id 
* @return Response 
*/ 
public function show($id) 
{ 
    return \View::make('admin.pages.show')->with('page', Page::find($id)); 
} 

/** 
* Show the form for editing the specified resource. 
* 
* @param int $id 
* @return Response 
*/ 
public function edit($id) 
{ 
    return \View::make('admin.pages.edit')->with('page', Page::find($id)); 
} 

/** 
* Update the specified resource in storage. 
* 
* @param int $id 
* @return Response 
*/ 
public function update($id) 
{ 
    $validation = new PageValidator; 

    if ($validation->passes()) 
    { 
     // $file = Input::file('file'); 
     // if(Input::upload('file', 'public/images', $file['name'])) { 
     // $image = Response::json(array('filelink' => 'images/' . $file['name'])); 
     // } 
     $page = Page::find($id); 
     $page->title = Input::get('title'); 
     $page->slug = Str::slug(Input::get('title')); 
     $page->body = Input::get('body'); 
     $page->user_id = Sentry::getUser(); 



     $page->save(); 

     return Redirect::route('pages.edit', $page->id); 
    } 

    return Redirect::back()->withInput()->withErrors($validation->errors); 
} 

/** 
* Remove the specified resource from storage. 
* 
* @param int $id 
* @return Response 
*/ 
public function destroy($id) 
{ 
    $page = Page::find($id); 
    return Redirect::route('admin.pages.pages'); 
} 

} 

我的问题:

在标记,我在哪里瞄准imageUpload? pages.edit? 在控制器中,放置获取Redactor的imageUpload所需的代码的工作是在哪里进行的?

谢谢!

回答

2

对于使用redactor的imageupload功能,您必须为其编写自定义方法。我装戴例子

public function postUpload(){ 

    $file = Input::file('file'); 

    $destinationPath = 'uploads/'; 
    $extension = $file->getClientOriginalExtension(); 
    $filename = uniqid($file->getFilename()) . '.' . $extension; 
    $upload_success = Input::file('file')->move($destinationPath, $filename); 

    if($upload_success) { 
     return Response::json($data = array(
      'filelink' => Request::root() . '/' .$destinationPath . $filename 
     ), 200); 
    } else { 
     return Response::json('error', 400); 
    } 
} 

主编会变成这样的事情:

$('#redactor').redactor({ 
imageUpload: base + '/url/to/controller' 
}); 

欲了解更多信息,你可以看到这篇 http://laravel.com/docs/requests#files

+0

您的解决方案已经帮了不少忙。我现在唯一的问题是,我得到了一个破碎的图像结果...如果我右键点击,获取图像的URL,这是正确的。它也正确地保存图像。我唯一能想到的是导致这个问题的原因是我在源代码中得到了这个时髦的URL {“filelink”:“http:\/\/localhost:8000 \/public \/uploads \ /phpgldSn851f8cff91ff61.jpg”我的网页开发工具...任何想法,为什么这是?谢谢 –

+0

如果可以的话,我会为你投票......我也在想,我是否需要为上传目录声明路线?这就是为什么它没有显示?谢谢 –

+0

为每个图像获取这样的错误:GET http:// localhost:8000/public/uploads/phpYagTHb51f8cec69fa85.jpg 500(内部服务器错误) –

相关问题