0

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

Jone
  • 27
  • 7

1 Answers1

0

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);
} 
Community
  • 1
  • 1
Sam
  • 20,096
  • 2
  • 45
  • 71
  • Thanks for help i will try it now and i will look in that url that you give to me and Thanks for helping me i will be back as soon as i done from testing the codes – Jone Apr 11 '14 at 17:18
  • Let me know how it goes and I can keep giving advice with problems. – Sam Apr 11 '14 at 17:22
  • Okay Sam Sorry for late replay but i had so much work in my office i will replay how its goes today – Jone Apr 13 '14 at 10:31
  • every thing is okay when i use the Controller Now i am going to validate the Input of url – Jone Apr 13 '14 at 11:49
  • Thanks for the help and sorry for my late replay – Jone Apr 13 '14 at 11:58
  • will i have an error called unexpected T_IF after typing this code ' $validation = Validator::make(array( 'url' => $url ), array( 'url' => 'required|url' )) if ( $validation->fails() ) { return 'faild'; } ' – Jone Apr 13 '14 at 12:10
  • @Jone, it looks like you are missing a`;` after the `Validator::make()` statement and before your conditional. – Sam Apr 14 '14 at 15:37