Php MVC indexController未使用正确的操作
我的IndexController中有两个操作Php MVC indexController未使用正确的操作,php,model-view-controller,autoload,Php,Model View Controller,Autoload,我的IndexController中有两个操作 public function indexAction() { $this->view->setVars([ 'name' => 'Stefan', ]); } public function testAction() { $this->view->setVars([ 'name' => 'testOutput', ]); } 何时调用我的索引页
public function indexAction()
{
$this->view->setVars([
'name' => 'Stefan',
]);
}
public function testAction()
{
$this->view->setVars([
'name' => 'testOutput',
]);
}
何时调用我的索引页
它会输出我在视图/index/index.php中设置的名称
<h1>Hello <?php echo $name ?></h1>
';
}
编辑:
我在索引后调用什么操作其实并不重要。它可以是index/asdsad
他仍然在进行主索引
它甚至没有说他找不到行动
编辑2:
var\u dump($url、$urlParts、$controllerName、$actionMethodName)的输出
你能放一个die($actionName)在自动加载器中编写>并检查得到的值是多少?所有内容都是索引。因此,如果$actionName
是索引
,$controller->$actionMethodName()
肯定会一直调用indexAction()
。那么我如何才能解决这个问题呢?我真的被困在这里了。我在哪里可以找到这个MVC,这样我就可以在我这边重现这个问题了?
<?php
// simple autoloader
spl_autoload_register(function ($className) {
if (substr($className, 0, 4) !== 'Mvc\\') {
// not our business
return;
}
$fileName = __DIR__.'/'.str_replace('\\', DIRECTORY_SEPARATOR, substr($className, 4)).'.php';
if (file_exists($fileName)) {
include $fileName;
}
});
// get the requested url
$url = (isset($_GET['_url']) ? $_GET['_url'] : '');
$urlParts = explode('/', $url);
// build the controller class
$controllerName = (isset($urlParts[0]) && $urlParts[0] ? $urlParts[0] : 'index');
$controllerClassName = '\\Mvc\\Controller\\'.ucfirst($controllerName).'Controller';
// build the action method
$actionName = (isset($urlParts[1]) && $urlParts[1] ? $urlParts[1] : 'index');
$actionMethodName = $actionName.'Action';
try {
if (!class_exists($controllerClassName)) {
throw new \Mvc\Library\NotFoundException();
}
$controller = new $controllerClassName();
if (!$controller instanceof \Mvc\Controller\Controller || !method_exists($controller, $actionMethodName)) {
throw new \Mvc\Library\NotFoundException();
}
$view = new \Mvc\Library\View(__DIR__.DIRECTORY_SEPARATOR.'views', $controllerName, $actionName);
$controller->setView($view);
$controller->$actionMethodName();
$view->render();
} catch (\Mvc\Library\NotFoundException $e) {
http_response_code(404);
echo 'Page not found: '.$controllerClassName.'::'.$actionMethodName;
} catch (\Exception $e) {
http_response_code(500);
echo 'Exception: <b>'.$e->getMessage().'</b><br><pre>'.$e->getTraceAsString().'</pre>';
}
string(0) ""
array(1) {
[0]=>
string(0) ""
}
string(5) "index"
string(11) "indexAction"