Php 在模型-视图-控制器框架内动态更新url
我自己做了一个MVC框架。我陷入了url更新的困境。这就是我尝试登录时发生的情况,例如:Php 在模型-视图-控制器框架内动态更新url,php,url,model-view-controller,Php,Url,Model View Controller,我自己做了一个MVC框架。我陷入了url更新的困境。这就是我尝试登录时发生的情况,例如: 当我按下login按钮时,URL如下所示:localhost/login\u controller/ 登录控制器将被调用并加载验证凭据的所需模型 模型将状态返回给控制器,控制器加载相应的视图。在我的例子中是profile.php url现在应该是这样的:localhost/profile/ $this->load->view(配置文件) 执行上述代码后,url保持不变localhost/Login\u Co
class router{
private $registry;
public function __construct($registry){
$this->registry = $registry;
}
public function getController(){
$url = $_GET['url'];
if($contoller_path){
include $controller_path; // if controller exits the path will be included
$url = new $url.Controller(); // instantiating the object for controller
$url->auth_login();
}
}
}
class Login_controller extends controller{
public function __construct($registry){
parent::_construct( $registry);
}
public function auth_login(){
$url = $_GET['url'];
$username = $_POST['username'];
$password = $_POST['password'];
if($model_path){
include $_path;
$this->registry->url = new $url.model();
$status = $this->registry->{url->auth_login}($username, $password);
if($status == 'success'){
//HERE....
// I want to change the url localhost/login_controller to localhost/profile/
$this->registry->template->show('profile');
}if($status == 'failure'){
$this->registry->template->show('index');
}
}
}
}
class template{
private $registry;
public function __construct($registry){
$this->registry = $registry;
}
public function show($view){
if(is_readable($view_path)){
include $view_path; // if view exists, it would load
}else{
include $index_path; // if view does not exists, index page loads
}
}
}
控制器/Login\u controller.php
class router{
private $registry;
public function __construct($registry){
$this->registry = $registry;
}
public function getController(){
$url = $_GET['url'];
if($contoller_path){
include $controller_path; // if controller exits the path will be included
$url = new $url.Controller(); // instantiating the object for controller
$url->auth_login();
}
}
}
class Login_controller extends controller{
public function __construct($registry){
parent::_construct( $registry);
}
public function auth_login(){
$url = $_GET['url'];
$username = $_POST['username'];
$password = $_POST['password'];
if($model_path){
include $_path;
$this->registry->url = new $url.model();
$status = $this->registry->{url->auth_login}($username, $password);
if($status == 'success'){
//HERE....
// I want to change the url localhost/login_controller to localhost/profile/
$this->registry->template->show('profile');
}if($status == 'failure'){
$this->registry->template->show('index');
}
}
}
}
class template{
private $registry;
public function __construct($registry){
$this->registry = $registry;
}
public function show($view){
if(is_readable($view_path)){
include $view_path; // if view exists, it would load
}else{
include $index_path; // if view does not exists, index page loads
}
}
}
模型/自动登录模型
class auth_login_model extends model{
public function __construct($registry){
parent::_construct( $registry);
}
public function auth_login($username, $password){
//here is the code to query db and all..
if($success){
return $success;
}else{
return $failure;
}
}
}
application/template.php
class router{
private $registry;
public function __construct($registry){
$this->registry = $registry;
}
public function getController(){
$url = $_GET['url'];
if($contoller_path){
include $controller_path; // if controller exits the path will be included
$url = new $url.Controller(); // instantiating the object for controller
$url->auth_login();
}
}
}
class Login_controller extends controller{
public function __construct($registry){
parent::_construct( $registry);
}
public function auth_login(){
$url = $_GET['url'];
$username = $_POST['username'];
$password = $_POST['password'];
if($model_path){
include $_path;
$this->registry->url = new $url.model();
$status = $this->registry->{url->auth_login}($username, $password);
if($status == 'success'){
//HERE....
// I want to change the url localhost/login_controller to localhost/profile/
$this->registry->template->show('profile');
}if($status == 'failure'){
$this->registry->template->show('index');
}
}
}
}
class template{
private $registry;
public function __construct($registry){
$this->registry = $registry;
}
public function show($view){
if(is_readable($view_path)){
include $view_path; // if view exists, it would load
}else{
include $index_path; // if view does not exists, index page loads
}
}
}
希望这足够详细说明。执行该代码后,URL应该是什么样子?另外,我们可能需要查看
view
方法的定义才能理解您的意思。@halfer我已经添加了链接。在阅读了本教程之后,我以同样的方式设计了框架。我编辑了这篇文章,并给出了完整的细节,以使您理解。