/** * Store a newly created resource in storage. * * @return Response */ public function store() { // Validate the url format and uniquiness. $validator = Validator::make(Request::all(), ['url' => 'required|url|unique:links']); // If doesn't validate, redirect to the previous page // With errors and input for correction if ($validator->fails()) { return redirect()->back()->withErrors($validator)->withInput(); } // Create an instance of the link model $link = new Link(); $link->url = Request::get('url'); // Gets page title from the website $page_title = Request::get('title'); // If can't recover the page title, show user a form to // Manually insert a page title if (empty($page_title)) { $page_title = $this->getPageTitleByUrl(Request::get('url')); if (empty($page_title)) { return redirect('link/create')->withInput()->withErrors($validator)->with('warning_message', 'You must manually add a title to this url!'); } } // Everything is allright, put page title // in the link object $link->title = $page_title; $link->user()->associate(Auth::user()); // Save everything in the database $link->save(); // Return to index page and show message return redirect('link')->with('flash_message', 'Link added with success!'); }