1

専用ルートを使用した 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 をどこに向ければよいですか? ページを編集しますか? Redactor の imageUpload を機能させるために必要なコードは、コントローラーのどこに配置しますか?

ありがとう!

4

1 に答える 1

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

于 2013-07-30T10:12:21.207 に答える