Okay after get almost every thing work in my code
and its pretty Good
i need help about how to validate the user url Input
and if he did insert a non Url how to foreword it to 404 page
and here is my 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 and return shortned url
$row = new URl;
$row->urls = $url;
$row->shortend = $shortend;
$row->save();
// create a results view and present the short url to the usr
if ($row){
return View::make('result')->with('shortend',$shortend);
}
});
Route::get('{shortend}', function($shortend)
{
// query the DB For the row with that short url
$row = Url::where('shortend', '=', $shortend)->first();
// if not found redirect to home page
if (is_null($row) ) return Redirect::to('/');
// else grab the url and redirect
return Redirect::to($row->urls);
});
forgive me for my many questions but i am totally new to laravel
First, as a tip, you have logic like this in your routes.php. A URL router is meant to take HTTP requests and "route" them to the correct controllers and methods. See the documentation to get started with controllers.
I am never a fan of "redirecting to a 404 page", instead I believe if a page isn't found it should display a 404 page there. To do this with laravel, you can call App::abort(404);
which will kill the application and return a 404 status to the browser. You can take this a step further by "listening" to 404 errors and returning your own custom view:
App::missing(function()
{
return View::make('errors/404');
});
Let me know if you need more help validating a URL, but I think you should start by restructuring your code by using controllers. You can check this question for regular expressions to match URLs. This is how you would use a regular expression in PHP:
if(!preg_match('/expression/', $url)) {
App::abort(404);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.