03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

File-Based CMS 329<br />

1 Route::any("admin/layout/add", [<br />

2 "as" => "admin/layout/add",<br />

3 "uses" => "LayoutController@addAction"<br />

4 ]);<br />

.<br />

This was extracted from app/routes.php.<br />

The form processing, in the addAction() method, is wrapped in a check for the save parameter. This<br />

is the name of the submit button on the add form. We specify the validation rules (including one of<br />

those we defined in the constructor). If validation fails, we redirect back to the add page, bringing<br />

along the errors and old input. If not, we create a new file with the default meta title and default<br />

meta description as metadata. Finally we redirect to the index page.<br />

The view is fairly standard (including the bootstrap tags we’ve used). The name and code fields have<br />

error messages and all of the fields have their values set to the old input values. We’ve also added a<br />

route to the add page.<br />

Edit follows a similar pattern:<br />

1 public function editAction()<br />

2 {<br />

3 $layout = Input::get("layout");<br />

4 $name = str_ireplace(".blade.php", "", $layout);<br />

5 $content = $this->filesystem->read("layouts/" . $layout);<br />

6 $extracted = $this->engine->extractMeta($content);<br />

7 $code = trim($extracted["template"]);<br />

8 $parsed = $this->engine->parseMeta($extracted["meta"]);<br />

9 $title = $parsed["title"];<br />

10 $description = $parsed["description"];<br />

11<br />

12 if (Input::has("save"))<br />

13 {<br />

14 $validator = Validator::make(Input::all(), [<br />

15 "name" => "required|edit:" . Input::get("layout"),<br />

16 "code" => "required"<br />

17 ]);<br />

18<br />

19 if ($validator->fails())<br />

20 {<br />

21 return Redirect::route("admin/layout/edit")<br />

22 ->withInput()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!