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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探