解决thinkphp5 只能访问首页问题

浏览:3915 发布日期:2019/03/20 分类:技术分享
thinkphp5或者thinkphp5.1开始用的时候,遇到过只能访问Index模块的index方法,其他模块的方法,或者Index模块的其他方法都不可以访问。

环境:
win7,phpstudy2018,php7.1,nginx

xxx.com可以访问
xxx.com/index.php可以访问
xxx.com/index.php/Index可以访问
xxx.com/index/php/Index/index可以访问

但别的方法全部都不可以访问,如:xxx.com/Index/one就不行了。
关键的必备条件是:
1.使用多模块的命名空间
2.加上thinkphp控制器的命名空间
3.继承thinkphp控制器

详细说明例子如下:(这样做,相当于不使用强制路由了)


thinkphp 5.1.35 控制器模块访问(解决只能访问Index/index问题)

环境:phpstudy 2018 ,php7.1,nginx


public/index.php文件内容:

<?php

namespace think;

require __DIR__.'/../thinkphp/base.php';

Container::get('app')->bind('home')->run()->send(); //关键是bind('home')


application/home/controller/Index.php内容:

<?php

namespace app\home\controller; //关键是:\home\

use think\Facade\Config;

use think\Route;

use think\Db;

use think\Controller; //关键是:use think\Controller;


class Index extends Controller { //关键是:Index extends Controller,针对的是twoThree()的return view();

public function index(){

echo "home application, Index controller, index action";

}


//让其他方法可以访问

public function one(){

echo "home application, Index controller, one action";

}


//驼峰模板处理办法,twoThree()方法,对应application/home/view/index/two_three.html模板,注意/index/要小写,two_three要小写并用下划线

public function twoThree(){

return view();

}


}



程序文件:

application/home/controller/Index.php

模板文件:

application/home/view/index/two_three.html


访问方式:

xxx.com/index.php/Index/index

xxx.com/index.php/Index/one

xxx.com/index.php/Index/twoThree

原文链接地址:http://qp500.cn/groupby/Blog/16829

最佳答案
评论() 相关
后面还有条评论,点击查看>>