2013-02-11 63 views
-1

我有一个在extjs 4中创建的登录视图,数据从mysql中获取。 我的问题是,我无法在成功登录后重定向我的页面。在登录extjs之后加载新视图4 mvc

我的代码重定向是。

onLoginSuccess : function(response, opts) 
         { 
          //Received response from the server 
          response = Ext.decode(response.responseText); 

          if(response.success) 
           { 

             var redirect = 'index';   
             window.location = redirect; 

           } 
          else 
           { 
            Ext.MessageBox.alert('Login failed', response.message); 
           } 
         } 
+0

如果您用''http:// yourdomain.com/index.html''替换''index''? – A1rPun 2013-02-11 10:55:26

+0

我认为与此页面将刷新到新视图,但我希望它加载视图,无需重新加载页面。 – anupkumar 2013-02-11 11:57:41

+0

window.location赋值总是重新加载页面。我想你需要另一种方法。 – A1rPun 2013-02-11 12:30:10

回答

3

有两种方法可以解决这个:

登录作为一个单独的页面

这是推荐的方式,速度更快,更安全。

  • 有一个index.php页面,用来检查用户是否已经登录。
  • 如果用户登录,您应该只需要您的实际系统索引文件,其中包括ExtJs标头。
  • 如果用户未登录,则应该要求一个显示实际登录屏幕的login.php文件。这个页面可能会,也可能不会加载ExtJs库(因为这个页面上很少,我假设你不需要这里的ExtJs文件)。

因此,举例来说,这是我的index.php:

<?php 

require_once('common/include/User.php'); 

if (SessionUser()->IsLoggedIn()) 
{ 
    // chdir is simply to keep the correct paths when compiling the app. 
    // It works for php, but for html/css links you should use the base tag 
    // in your php/html file: <base href="app/"/> 
    chdir('app'); 
    require_once('app/index.php');  
} else { 
    require_once('login.php'); 
} 

?> 

然后app/index.php是装入您的应用程序的脚本和ExtJS的lib中的实际索引文件。

login.php仅仅是一个相当简单的登录表单:从ExtJS的应用

这种方法中

<?php 
// Index file for logged out users (guests) 

$iUserName = isset($_POST['username']) ? $_POST['username'] : ''; 

$iLoginErrorTxt = ''; 

if (isset($_POST['username'])) 
{ 
    require_once('common/include/User.php'); 

    $iLoginError = SessionUser()->Authenticate($_POST['username'], $_POST['password']); 

    if ($iLoginError['success']) 
    { 
     // Login successful - reload the page. 
     header("Location: " . $_SERVER['PHP_SELF']); 
     exit(); 
    } else { 
     // Login failed - present an error. 
     $iLoginErrorTxt = $iLoginError['error']; 
    } 
} 
?> 


<html> 
<head> 
    <title>My System</title>    
</head> 

<body> 

<form class="login-form" action="<?=$_SERVER['PHP_SELF']?>" enctype="application/x-www-form-urlencoded" method="post"> 

    <input name="username" size="25" type="text" value="<?=$iUserName?>" value spellcheck="false" placeholder="User Name"/> 

    <input name="password" size="25" type="password" value spellcheck="false" placeholder="Password"/> 

    <div class="error-message"><?=$iLoginErrorTxt?></div> 

    <input name="submit" type="submit" value="Login" /> 

</form> 

</body> 
</html> 

登录不强烈建议,因为你需要加载整个ExtJS的框架,并很可能您的应用程序脚本,甚至在用户认证之前。

一个可能的实现将涉及一个容器面板,它一次只显示一个面板,可以是登录页面,也可以是实际的应用程序页面。

的app.js可能包括下面的代码:

refs: 
[{ 
    ref: 'contentPanel', 
    selector: 'viewport > #contentPanel' 
}], 


controllers: [ 
    'MainMenu' 
], 

launch: function() { 
    // Enable quicktips 
    Ext.QuickTips.init(); 

    // Create the viewport 
    Ext.create('Ext.container.Viewport', { 
     layout: 'fit', 
     items: [ 
      { 
       xtype: 'panel', 
       id: 'contentPanel', 
       layout: 'card', 

       dockedItems: [{ 
        xtype: 'mainmenu', 
        dock: 'top' 
       }] 
      } 
     ] 
    }); 
}, 

然后,你可以这样做:

var iContentPanel = this.getContentPanel(); 
iContentPanel.getLayout().setActiveItem(iPage); 

哪里iPage是要显示任何页面(面板)。

有很明显的方法可以改善它的工作方式,例如通过动态加载控制器;但这是我相信的另一个问题的故事。

无论如何,我强烈建议你考虑第一种方法。

+0

我想到了第二种方法“这种方法不被强烈推荐”,它不正确。如果我有公司内部管理后端,这可能是一种更好的解决方案,因为您可以处理到期会话更加用户友好。 – 2013-02-12 13:05:17