Skip to content

PHP Laravel 5 controller example

Eric Jacobsen edited this page May 15, 2015 · 9 revisions

`
<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Input;
use Validator;

class ImageController extends Controller {
	/**
	 * Store an image.
	 *
	 * @return simple JSON response message
	 */
	public function store(Request $r)
	{
            $image = Input::file('file');
            $validator = Validator::make([$image], ['image' => 'required']);
            if ($validator->fails()) {
        	return $this->errors(['message' => 'Not an image.', 'code' => 400]);
            }
            $destinationPath = storage_path() . '/uploads';
            if(!$image->move($destinationPath, $image->getClientOriginalName()) {
        	return $this->errors(['message' => 'Error saving the file.', 'code' => 400]);
            }
            return response()->json(['success' => true], 200);
	}
}

`

Clone this wiki locally