Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel 4.1验证url输入_Php_Laravel - Fatal编程技术网

Php laravel 4.1验证url输入

Php laravel 4.1验证url输入,php,laravel,Php,Laravel,好吧,在我的代码中几乎每件事都完成之后 而且相当不错 我需要关于如何验证用户url输入的帮助 如果他真的插入了一个非Url,那么如何在404页上添加前言呢 这是我的路线文件 Route::get('/', function() { return View::make('hello'); }); Route::post('/', function() { $url = Input::get('url'); $record = Url::where('urls', '=', $

好吧,在我的代码中几乎每件事都完成之后

而且相当不错

我需要关于如何验证用户url输入的帮助

如果他真的插入了一个非Url,那么如何在404页上添加前言呢

这是我的路线文件

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);

    });

请原谅我的许多问题,但我对laravel完全陌生。首先,作为提示,您的routes.php中有这样的逻辑。URL路由器用于接收HTTP请求并将其“路由”到正确的控制器和方法。请参阅以开始使用控制器

我从不喜欢“重定向到404页面”,相反,我相信如果找不到页面,它应该在那里显示404页面。要对laravel执行此操作,可以调用
App::abort(404)将终止应用程序并将404状态返回到浏览器。通过“监听”404错误并返回自己的自定义视图,您可以更进一步:

App::missing(function()
{
    return View::make('errors/404');
});
如果您需要更多验证URL的帮助,请告诉我,但我认为您应该首先使用控制器重新构造代码。您可以检查正则表达式以匹配URL。以下是在PHP中使用正则表达式的方式:

if(!preg_match('/expression/', $url)) {
    App::abort(404);
} 

谢谢你的帮助,我现在就试试,我会看看你给我的网址,谢谢你的帮助,我一测试完代码就会回来,让我知道代码是怎么回事,我可以继续给问题提建议。好的,山姆,很抱歉,我的办公室有很多工作,我会重播它今天的运行情况好的,当我现在使用控制器时,我将验证URL的输入谢谢你的帮助,很抱歉我的延迟重播