首页 > php laravel5问题求助。

php laravel5问题求助。

1.resource controller

php artisan make:controller创建controller之后有一个show方法,

public function show($id=0)
    {
        $page=Page::find($id);
        print_r($page);
    }

如何设置ID必须为数字,除了在方法里面?
因为这是用于资源型的路由,不输ID不走show方法到index里面,若是我用于其他控制器的getShow($id),
这样要是没输id会报错,只能这样指定一个默认值来避免吗?getShow($id=0),

2.命名空间/API的使用问题

在API中有很多方法,该如何使用?

比如说http://laravel.com/api/5.1/Illuminate/Http/Request.html#method_ip

echo \Requst::ip(); 这么用报错了,能不用use引入吗?很多时候记不住

string ip()

Returns the client IP address.

Return Value

string

自定义错误的处理

指定一个路由只能为数字之后用户输了不是数字的就报错了,怎么让其提示不存在的,或者跳转到指定页面


1.如果使用Route::resource(),你可以尝试在RouteServiceProviderboot()方法增加:

public function boot(Router $router)
{
    $router->pattern('id', '[0-9]+');

    parent::boot($router);
}

注意,引入Router。

2.既然记不住,请使用一个好的IDE,比如PHPStorm,相关教程

https://laravist.com/series/phpstorm-the-best-php-ide-you-ever-met

3.自定义404能满足你的需求不?

【热门文章】
【热门文章】