Question

I've seen several solutions for the Redactor imageUpload using a dedicated route, however I choose to use a resource controller to do all the routing.

The markup:

<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>

The code:

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');
}

}

My questions:

In the markup, where do I aim the imageUpload? pages.edit? In the controller, where do put the code that is required to get Redactor's imageUpload to work?

Thanks!

Was it helpful?

Solution

For using redactor's imageupload feature, you have to write a custom method for it. I've attached an example

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 will become something like this:

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

For more information you can read this http://laravel.com/docs/requests#files

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top