dsfsad089111 2013-12-01 07:35
浏览 15
已采纳

“Laravel”中php命名空间的问题

I use subfolder in the Controller 'folder',which works fine.. but when I write the blow code ..php return the error said "Auth is not found ,and the Input'

<?php
namespace website;


use Auth;
use Input;
use View;
use Illuminate\Routing\Controllers\Controller;

class HomeController extends Controller {

    public function index()
    {
        return View::make('wcsite.index');
    }

    public function saveHome()
    {
        $uid = Auth::user()->id;
        $websiteData = Input::get('data');
        return $uid;
    }
}

but when I add 'use Auth,use Input',everything works fine...so ,anyone who can tell me ...is there any way to to this ,which "need not to use Auth,use Input in my subfolder Controllers' Thank you a lot!

and my route is

Route::post('/wcsite',array('uses' => 'website\HomeController@saveHome'))->before('auth');
  • 写回答

1条回答 默认 最新

  • doubu4406 2013-12-01 07:48
    关注

    Your question is a bit confusing. You're saying that the code above is not working because PHP can't find the Auth and Input global class references but your code clearly shows you're importing them correctly.

    PHP can't use the global Auth and Input class references without importing them first (which you're doing in the above code). It's going to assume they're located under the website namespace by default.

    If you don't want to import hem with use statements you could always reference the global namespace by using a backslash before the class name like the code below:

    <?php
    namespace website;
    
    use Illuminate\Routing\Controllers\Controller;
    
    class HomeController extends Controller {
    
        public function index()
        {
            return \View::make('wcsite.index');
        }
    
        public function saveHome()
        {
            $uid = \Auth::user()->id;
            $websiteData = \Input::get('data');
    
            return $uid;
        }
    }
    

    That being said, I prefer importing the classes first instead of using backslashes everywhere. It'll provide for much cleaner code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部