我有这个MVC文件夹结构:
application
-----------catalog
------------------controller
----------------------------ProfileContoller.php
------------------model
----------------------------UserModel.php
------------------view
------------------language
-----------admin
------------------controller
------------------model
------------------view
------------------language
core
---- Controller.php
public
vendor
....
现在,在ProfileController.php中,我有:
namespace Application\Catalog\Controller;
use Application\Core\Controller as Controller;
use Application\Core\Model\UserModel;
class ProfileController extends Controller
{
/**
* Construct this object by extending the basic Controller class
*/
public function __construct()
{
parent::__construct();
}
/**
* This method controls what happens when you move to /overview/index in your app.
* Shows a list of all users.
*/
public function index()
{
$this->View->render('profile/index','', array(
'users' => UserModel::getPublicProfilesOfAllUsers())
);
}
}
在usermodel.php中,我有:
namespace Application\Catalog\Model;
class UserModel
{
public static function getPublicProfilesOfAllUsers()
{
$database = DatabaseFactory::getFactory()->getConnection();
$sql = "SELECT user_id, user_name, user_email, user_active, user_has_avatar, user_deleted FROM users";
$query = $database->prepare($sql);
$query->execute();
$all_users_profiles = array();
foreach ($query->fetchAll() as $user) {
array_walk_recursive($user, 'Filter::XSSFilter');
$all_users_profiles[$user->user_id] = new stdClass();
$all_users_profiles[$user->user_id]->user_id = $user->user_id;
$all_users_profiles[$user->user_id]->user_name = $user->user_name;
$all_users_profiles[$user->user_id]->user_email = $user->user_email;
$all_users_profiles[$user->user_id]->user_active = $user->user_active;
$all_users_profiles[$user->user_id]->user_deleted = $user->user_deleted;
$all_users_profiles[$user->user_id]->user_avatar_link = (Config::get('USE_GRAVATAR') ? AvatarModel::getGravatarLinkByEmail($user->user_email) : AvatarModel::getPublicAvatarFilePathOfUser($user->user_has_avatar, $user->user_id));
}
return $all_users_profiles;
}
并使用Composer Psr4 I自动加载文件:
"autoload": {
"psr-4": { "Application\\": "application/","Application\\Core\\": "application/core/","Application\\Catalog\\Model\\": "application/catalog/model/"}
}
}
现在,当我需要路由我的URL时,我会采取行动:
$route->get('/cms/profile/index/', 'Application\Catalog\Controller\ProfileController@index');
我看到这个错误:
>致命错误:未捕获错误:在C:\XAMPP\HTDOCS\CMS\Application\Catalog\Controller\ProfileController.php:25堆栈跟踪:#0[内部函数]:Application\Catalog\Controller\ProfileController->索引()#1 C:\XAMPP\HTDOCS\CMS\Vendor\Router\Route\System\Route.php(649):call_user_func_array(数组,数组)#2
在这里,您调用的函数的类名为model class在控制器中不存在。UserModel::GetPublicProfileSofAllUsers()
使用Application\Controller\Model\UserModel作为
或
use Application\Controller\Model\UserModel;
class ProfileController extends Controller
{
protected $userModel;
/**
* Construct this object by extending the basic Controller class
*/
public function __construct()
{
parent::__construct();
$this->load->model('UserModel');
}
您可以调用为
$this->View->render('profile/index','', array(
'users' => $this->UserModel->getPublicProfilesOfAllUsers())
);
最好的方法是通过对象调用非静态函数。在PHP5.3中,我们可以将非静态函数作为静态函数,但在PHP7+中,它被标记为不推荐使用,并将在未来删除它。