dsfsad089111 2013-12-01 15: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 15: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.

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

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳