【问题标题】:Redactor imageUpload and Laravel 4 Resource ControllersRedactor imageUpload 和 Laravel 4 资源控制器
【发布时间】:2013-07-30 08:57:31
【问题描述】:

我已经看到了几种使用专用路由的 Redactor imageUpload 解决方案,但是我选择使用资源控制器来完成所有路由。

标记:

<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 工作所需的代码放在哪里?

谢谢!

【问题讨论】:

    标签: laravel wysiwyg image-uploading redactor


    【解决方案1】:

    要使用 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').redactor({
    imageUpload: base + '/url/to/controller'
    });
    

    有关更多信息,您可以阅读此内容 http://laravel.com/docs/requests#files

    【讨论】:

    • 您的解决方案帮助很大。我现在唯一遇到的问题是,我得到了一个损坏的图像结果......如果我右键单击,获取图像 URL,它是正确的。它也可以正确保存图像。我认为导致此问题的唯一原因是我在我的网络开发工具...知道为什么吗?谢谢
    • 顺便说一句,如果可以的话,我会投票给你...我也在想,我是否可能需要为上传目录声明一个路由?这就是不显示的原因吗?谢谢
    • 为每个图像获取这样的错误:GET localhost:8000/public/uploads/phpYagTHb51f8cec69fa85.jpg 500 (Internal Server Error)
    • 知道了。控制器不需要绝对路径!再次感谢所有的帮助! return Response::json($data = array( 'filelink' => '/uploads/' . $filename ), 200);
    • 在我的情况下确实如此,但如果它对你来说很好:)(抱歉迟到的回复)
    猜你喜欢
    • 1970-01-01
    • 2016-05-21
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2014-05-29
    • 1970-01-01
    相关资源
    最近更新 更多