我已经看到使用专用路由的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工作所需的代码?
谢谢!
答案 0 :(得分: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').redactor({
imageUpload: base + '/url/to/controller'
});
有关详细信息,请阅读此内容 http://laravel.com/docs/requests#files