Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.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按1d显示查询用户_Php_Laravel 4 - Fatal编程技术网

Php laravel 4按1d显示查询用户

Php laravel 4按1d显示查询用户,php,laravel-4,Php,Laravel 4,我正在使用Laravel4进行一个web项目,我正在创建一个管理面板 在admin中,我使用admin/profile/{id}来显示用户配置文件,如firstname、lastname等 在我的AdminController中,我有: // get the Admin Profile page public function getProfile($id) { // get the user from the database $user = User::f

我正在使用Laravel4进行一个web项目,我正在创建一个管理面板

在admin中,我使用admin/profile/{id}来显示用户配置文件,如firstname、lastname等

在我的AdminController中,我有:

     // get the Admin Profile page
    public function getProfile($id) {

    // get the user from the database
    $user = User::find($id);
    $this->layout->content = View::make('admin.profile', array('user' => $user));

    }
但是,如果我只是在没有任何用户id的情况下转到admin/profile,会发生什么情况,我如何让它工作


基本上,如果页面不存在,如何转到仪表板或类似的东西?例如,如果他们尝试了admin/test,而test不是其中的一种方法,如果他们登录,它将转到仪表板,如果不是,它将转到登录页面?

您问了两个问题:

  • 如果我在没有用户ID的情况下转到
    admin/profile
    ,会出现404错误。如何重定向到登录页面

  • 如果用户未登录,如何重定向到登录页面

  • 对于第一个问题,你可以用两种方法来解决。一种解决方案是在所有其他路由定义之后添加与任何内容匹配的路由:

    Route::get('{any_url}', function(){ return Redirect::route("login"); });
    
    必须是定义的最后一个路由,因为它将匹配任何URL

    另一种方法是在
    start/global.php
    文件中捕获
    NotFoundHttpException
    。添加此代码:

    App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
    {
        return Redirect::route("login");
    });
    
    这两个示例都重定向到名为
    login
    的命名路由

    至于第二个问题,正确的处理方法是使用
    auth
    过滤器。在
    filters.php
    文件中,可以添加如下内容:

    Route::filter('auth', function($route)
    {
        // is the user authorized? if not, redirect to the login page
        if (!user_is_authorized())
        {
                // redirect to the login page
                return Redirect::route('login');
        }
    });
    
    App::missing(function($e){
        // Log the missing url
        Log::error($e);
    
        // You may redirect to home
        return Redirect::to('/');
    
        // Or redirect to a 404 route that is declared
        // to show a custom 404 page from that route
        return Redirect::to('missing');
    });
    
    Route::get('missing', function(){
        // show the view: errors.missing
        $this->layout->content = View::make('errors.missing');
    });
    

    user\u被授权时
    功能只是您在代码中执行的任何检查的简写。有关使用
    auth
    过滤器的信息,请参阅。

    您只需添加一个
    缺失的
    处理程序(句柄
    404
    ),如下所示:

    Route::filter('auth', function($route)
    {
        // is the user authorized? if not, redirect to the login page
        if (!user_is_authorized())
        {
                // redirect to the login page
                return Redirect::route('login');
        }
    });
    
    App::missing(function($e){
        // Log the missing url
        Log::error($e);
    
        // You may redirect to home
        return Redirect::to('/');
    
        // Or redirect to a 404 route that is declared
        // to show a custom 404 page from that route
        return Redirect::to('missing');
    });
    
    Route::get('missing', function(){
        // show the view: errors.missing
        $this->layout->content = View::make('errors.missing');
    });
    
    将代码(上面给出的)放入
    app/start/global.php
    文件中。对于
    缺失的
    url/route
    您需要在
    routes.php
    文件中添加一条路径,如下所示:

    Route::filter('auth', function($route)
    {
        // is the user authorized? if not, redirect to the login page
        if (!user_is_authorized())
        {
                // redirect to the login page
                return Redirect::route('login');
        }
    });
    
    App::missing(function($e){
        // Log the missing url
        Log::error($e);
    
        // You may redirect to home
        return Redirect::to('/');
    
        // Or redirect to a 404 route that is declared
        // to show a custom 404 page from that route
        return Redirect::to('missing');
    });
    
    Route::get('missing', function(){
        // show the view: errors.missing
        $this->layout->content = View::make('errors.missing');
    });
    
    创建一个
    视图
    作为
    视图/错误/missing.blade.php
    并在
    missing.blade.php
    视图中,显示一条奇特的消息,通知访问者,
    请求的页面/url
    不可用,并在该
    404
    页面中添加到主页的链接


    请阅读Laravel网站上关于的更多信息,检查。

    对于第一个问题,如果我添加NotFoundHttpException并不是针对我的整个应用程序,因此每当出现错误时,它将被重定向到登录页面??我是否可以将代码添加到我的getProfile($id)中,其中id未设置,例如重定向到仪表板?这可能吗?是的,
    NotFoundHttpException
    将应用于整个应用程序。如果你不想那样,那就做点别的吧。对于您的第二条评论,是的,如果您在路由中设置
    {id}
    参数为可选的话,这也会起作用;如何使其成为可选的?我在AdminController中尝试过这一点:公共函数getProfile($id){if(!isset($id))Redirect::to('admin/dashboard');$user=user::find($id);$this->layout->content=View::make('admin.profile',array('user'=>$user));}但仍然出现错误:AdminController::getProfile()缺少参数1