php - laravel 4.1 validate url input -
okay after every thing work in code
and pretty
i need how validate user url input
and if did insert non url how foreword 404 page
and here route file
route::get('/', function() { return view::make('hello'); }); route::post('/', function() { $url = input::get('url'); $record = url::where('urls', '=', $url)->first(); if ($record) { return view::make('result') ->with('shortend', $record->shortend); } function get_uniqe_short_url() { $shortend = base_convert(rand(1000,99999), 10, 36); if (url::where('shortend', '=' ,$shortend)->first() ){ get_uniqe_short_url(); } return $shortend; } $shortend = get_uniqe_short_url(); // otherwise add new row , return shortned url $row = new url; $row->urls = $url; $row->shortend = $shortend; $row->save(); // create results view , present short url usr if ($row){ return view::make('result')->with('shortend',$shortend); } }); route::get('{shortend}', function($shortend) { // query db row short url $row = url::where('shortend', '=', $shortend)->first(); // if not found redirect home page if (is_null($row) ) return redirect::to('/'); // else grab url , redirect return redirect::to($row->urls); });
forgive me many questions totally new laravel
first, tip, have logic in routes.php. url router meant take http requests , "route" them correct controllers , methods. see the documentation started controllers.
i never fan of "redirecting 404 page", instead believe if page isn't found should display 404 page there. laravel, can call app::abort(404);
kill application , return 404 status browser. can take step further "listening" 404 errors , returning own custom view:
app::missing(function() { return view::make('errors/404'); });
let me know if need more validating url, think should start restructuring code using controllers. can check this question regular expressions match urls. how use regular expression in php:
if(!preg_match('/expression/', $url)) { app::abort(404); }
Comments
Post a Comment