【问题标题】:MVC: How to redirect a view to a controller which will go in another view?MVC:如何将视图重定向到将进入另一个视图的控制器?
【发布时间】:2011-07-21 17:54:21
【问题描述】:

我在 PHP + JS + HTML + CSS 中遵循 MVC 模型 另外,我使用 CodeIgniter 作为框架。

我想知道,我需要做什么才能重定向到另一个控制器。我在我的项目的主页中.. 当我单击产品的“详细信息”(div = prod_details)时,应该是什么参考。

寻求帮助,这是我的代码

我的家控制器

class ordiDepot extends Controller {
function ordiDepot(){
    parent::Controller();
}
function index(){


    $data['base'] = $this->config->item('base_url');
    $data['css'] = $this->config->item('chemin').'views/style.css';
    $data['image'] = $this->config->item('chemin').'views/images';
    $data['js'] = $this->config->item('chemin').'views/boxOver.js';
    $data['abs'] = 'ordiDepot/accueil';
    $this->load->view('ordiDepotH',$data);
    $this->load->view('ordiDepotB',$data);
    $this->load->view('ordiDepotF',$data);

    }

    function accueil($page){
    $data['details_page'] = $this->config->item($page);
    $data['css'] = $this->config->item('chemin').'views/style.css';
    $this->load->view('details.php',$data);

            }
    }

我的观点

<div class="left_content">
<div class="title_box">Pieces</div>

    <ul class="left_menu">
        <li class="odd"><a href="services.html">Processeurs</a></li>
        <li class="even"><a href="services.html">Carte Mere</a></li>
        <li class="odd"><a href="services.html">Carte Video</a></li>
        <li class="even"><a href="services.html">Carte Reseau</a></li>
        <li class="odd"><a href="services.html">Bloc d'alimentation</a></li>
        <li class="even"><a href="services.html">Disque dur</a></li>
        <li class="odd"><a href="services.html">Boitiers</a></li>
    </ul> 


 <div class="title_box">Produit en special</div>  
 <div class="border_box">
     <div class="product_title"><a href="details.html">Motorola 156 MX-VL</a></div>
     <div class="product_img"><a href="details.html"><img src= <?php echo $image?>/laptop.png alt="" title="" border="0" /></a></div>
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
 </div>  

感谢大家

 <div class="banner_adds">

 <a href="#"><img src="images/bann2.jpg" alt="" title="" border="0" /></a>
 </div>    

新产品

    <div class="prod_box">
        <div class="top_prod_box"></div>
        <div class="center_prod_box">            
             <div class="product_title"><a href="details.html">Motorola 156 MX-VL</a></div>
             <div class="product_img"><a href="details.html"><img src=<?php echo $image?>/laptop.gif alt="" title="" border="0" /></a></div>
             <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>                        
        </div>
        <div class="bottom_prod_box"></div>             
        <div class="prod_details_tab">
        <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="<?php echo $image;?>/cart.gif" alt="" title="" border="0" class="left_bt" /></a>

        <a href="details.php" class="prod_details">details</a>            
        </div>                     
    </div>



    <div class="prod_box">
        <div class="top_prod_box"></div>
        <div class="center_prod_box">            
             <div class="product_title"><a href="details.html">Ipod</a></div>
             <div class="product_img"><a href="details.html"><img src= <?php echo $image?>/p4.gif alt="" title="" border="0" /></a></div>
             <div class="prod_price"><span class="price">270$</span></div>                        
        </div>
        <div class="bottom_prod_box"></div>             
        <div class="prod_details_tab">
        <a href="#" title="header=[Ajouter panier] body=[&nbsp;] fade=[on]"><img src="<?php echo $image;?>/cart.gif" alt="" title="" border="0" class="left_bt" /></a>

        <a href="details.html" class="prod_details">details</a>             
        </div>                     
    </div>

    <div class="prod_box">
        <div class="top_prod_box"></div>
        <div class="center_prod_box">            
             <div class="product_title"><a href="details.html">Webcam Samsung</a></div>
             <div class="product_img"><a href="details.html"><img src=<?php echo $image?>/p5.gif alt="" title="" border="0" /></a></div>
             <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>                        
        </div>
        <div class="bottom_prod_box"></div>             
        <div class="prod_details_tab">
        <a href="#" title="header=[Ajouter panier] body=[&nbsp;] fade=[on]"><img src="<?php echo $image;?>/cart.gif" alt="" title="" border="0" class="left_bt" /></a>

        <a href="details.html" class="prod_details">details</a>            
        </div>                     
    </div> 

    <div class="prod_box">
        <div class="top_prod_box"></div>
        <div class="center_prod_box">            
             <div class="product_title"><a href="<?php echo $abs;?>/details">Motorola 156 MX-VL</a></div>
             <div class="product_img"><a href="details.html"><img src=<?php echo $image?>/laptop.gif alt="" title="" border="0" /></a></div>
             <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>                        
        </div>
        <div class="bottom_prod_box"></div>             
        <div class="prod_details_tab">
        <a href="#" title="header=[Ajouter panier] body=[&nbsp;] fade=[on]"><img src="<?php echo $image;?>/cart.gif" alt="" title="" border="0" class="left_bt" /></a>

        <a href="details.html" class="prod_details">details</a>            
        </div>                     
    </div>



    <div class="prod_box">
        <div class="top_prod_box"></div>
        <div class="center_prod_box">            
             <div class="product_title"><a href="details.html">Iphone</a></div>
             <div class="product_img"><a href="details.html"><img src=<?php echo $image?>/p4.gif alt="" title="" border="0" /></a></div>
             <div class="prod_price"><span class="price">270$</span></div>                        
        </div>
        <div class="bottom_prod_box"></div>             
        <div class="prod_details_tab">
        <a href="#" title="header=[Ajouter panier] body=[&nbsp;] fade=[on]"><img src=<?php echo $image;?>/cart.gif alt="" title="" border="0" class="left_bt" /></a>

        <a href="details.html" class="prod_details">details</a>             
        </div>                     
    </div>

    <div class="prod_box">
        <div class="top_prod_box"></div>
        <div class="center_prod_box">            
             <div class="product_title"><a href="details.html">Webcam Samsung</a></div>
             <div class="product_img"><a href="details.html"><img src=<?php echo $image?>/p5.gif alt="" title="" border="0" /></a></div>
             <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>                        
        </div>
        <div class="bottom_prod_box"></div>             
        <div class="prod_details_tab">
        <a href="#" title="header=[Ajouter panier] body=[&nbsp;] fade=[on]"><img src= <?php echo $image;?>/cart.gif alt="" title="" border="0" class="left_bt" /></a>

        <a href="details.html" class="prod_details">details</a>            
        </div>                     
    </div> 
配饰
    <div class="prod_box">
        <div class="top_prod_box"></div>
        <div class="center_prod_box">            
             <div class="product_title"><a href="details.html">Motorola 156 MX-VL</a></div>
             <div class="product_img"><a href="details.html"><img src= <?php echo $image?>/laptop.gif alt="" title="" border="0" /></a></div>
             <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>                        
        </div>
        <div class="bottom_prod_box"></div>             
        <div class="prod_details_tab">
        <a href="#" title="header=[Ajouter panier] body=[&nbsp;] fade=[on]"><img src="<?php echo $image;?>/cart.gif" alt="" title="" border="0" class="left_bt" /></a>

        <a href="details.html" class="prod_details">details</a>            
        </div>                     
    </div>



    <div class="prod_box">
        <div class="top_prod_box"></div>
        <div class="center_prod_box">            
             <div class="product_title"><a href="details.html">Iphone</a></div>
             <div class="product_img"><a href="details.html"><img src=<?php echo $image?>/p4.gif alt="" title="" border="0" /></a></div>
             <div class="prod_price"><span class="price">270$</span></div>                        
        </div>
        <div class="bottom_prod_box"></div>             
        <div class="prod_details_tab">
        <a href="#" title="header=[Ajouter panier] body=[&nbsp;] fade=[on]"><img src="<?php echo $image;?>/cart.gif" alt="" title="" border="0" class="left_bt" /></a>

        <a href="details.html" class="prod_details">details</a>             
        </div>                     
    </div>

    <div class="prod_box">
        <div class="top_prod_box"></div>
        <div class="center_prod_box">            
             <div class="product_title"><a href="details.html">Webcam Samsung</a></div>
             <div class="product_img"><a href="details.html"><img src= <?php echo $image?>/p5.gif alt="" title="" border="0" /></a></div>
             <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>                        
        </div>
        <div class="bottom_prod_box"></div>             
        <div class="prod_details_tab">
        <a href="#" title="header=[Ajouter panier body=[&nbsp;] fade=[on]"><img src= <?php echo $image;?>/cart.gif alt="" title="" border="0" class="left_bt" /></a>

        <a href="details.html" class="prod_details">details</a>            
        </div>                     
    </div> 

购物车

        <div class="cart_details">
        3 items <br />
        <span class="border_cart"></span>
        Total: <span class="price">350$</span>
        </div>

        <div class="cart_icon"><a href="#" title="header=[Au panier] body=[&nbsp;] fade=[on]"><img src= <?php echo $image;?>/shoppingcart.png alt="" title="" width="48" height="48" border="0" /></a></div>

    </div>


 <div class="title_box">Nouveaux</div>  
 <div class="border_box">
     <div class="product_title"><a href="details.html">Motorola 156 MX-VL</a></div>
     <div class="product_img"><a href="details.html"><img src= <?php                           echo          $image?>/p2.gif alt="" title="" border="0" /></a></div>
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
 </div>  



<div class="title_box">Logiciels</div>

    <ul class="left_menu">
        <li class="odd"><a href="services.html">Jeux</a></li>
        <li class="even"><a href="services.html">Utilitaires</a></li>
        <li class="odd"><a href="services.html">OS</a></li>
    </ul>      

 <div class="banner_adds">

 <a href="#"><img src=<?php echo $image?>/bann1.jpg alt="" title="" border="0" /></a>
 </div>        

【问题讨论】:

  • 这是 MVC 被滥用的一个很好的例子

标签: php javascript codeigniter http-redirect


【解决方案1】:

要重定向到另一个控制器,您只需将该控制器的 URL 路径作为锚点的 href。

<a href="/controller/action"></a>

产品描述页面示例:

<a href="/products/item/324235">Product</a>

其中 324235 是需要从服务器端代码注入到 href 中的任何给定产品编号。

抱歉,我没有正确查看您的代码,因此您的控制器和操作名称可能不同。

【讨论】:

  • 好吧,如果我想用 php 变量动态地做到这一点。我需要去另一个控制器...或来自控制器的另一个功能“accueil”..我应该怎么做?
  • 哦,在控制器的代码中,只需使用 redirect('/controller/action/');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 2017-09-25
  • 1970-01-01
  • 2016-09-06
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多