仅重定向管理模块

时间:2017-01-12 02:07:36

标签: php zend-framework zend-framework2

我的public function onBootstrap(MvcEvent $e) { $application = $e->getApplication(); $em = $application->getEventManager(); if(!SystemUtils::isApiRequest()){ $em->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this,'initUser'),10000); } } public function initUser(MvcEvent $e) { $isLoggedIn=false; // determined elsewhere $action = Pluto::registry('application_action'); if($action!=='login' && !$isLoggedIn){ $viewHelperManager = $e->getApplication()->getServiceManager()->get('ViewHelperManager'); $renderer = $viewHelperManager->getRenderer(); $url = $renderer->plutourl('login'); $response = $e->getApplication()->getServiceManager()->get('Response'); $response->getHeaders()->clearHeaders()->addHeaderLine('Location', $url); $response->setStatusCode(302)->sendHeaders(); exit(); } }

中包含以下代码
$sm = $e->getApplication()->getServiceManager();
    $router = $sm->get('router');
    $request = $sm->get('request');
    $matchedRoute = $router->match($request);

问题是这只能在管理模块中的页面内部激活而你没有登录但是它的每个请求都在运行,而不管代码在里面的模块

我想在访问管理模块时将此检查限制为。

更新

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate: START");

    setContentView(R.layout.activity_main);


    ConnectivityManager connMgr = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        LoaderManager loaderManager = getLoaderManager();
        loaderManager.initLoader(EARTHQUAKE_LOADER_ID, null, this);

    } else {

    }

    Log.i(TAG, "onCreate: END");

}


@Override
public Loader<List<News>> onCreateLoader(int id, Bundle args) {
    Log.i(TAG, "onCreateLoader: START");

    NewsLoader newsloader = new NewsLoader(this,URL);
    List<News> list = newsloader.getList();
    Log.i(TAG, "onCreateLoader: list: " + list);

    Log.i(TAG, "onCreateLoader: END");

    return newsloader;
}

返回我可以使用的管理员控制器类名,但我希望它适用于所有管理控制器类

2 个答案:

答案 0 :(得分:1)

您可以使用共享事件管理器附加到给定名称空间中的事件。在下面的示例中,我们仅为当前模块命名空间附加事件。

use Zend\Mvc\MvcEvent;

public function onBootstrap(MvcEvent $e)
{
    $app = $e->getApplication();
    $em  = $app->getEventManager()->getSharedManager();
    $sm  = $app->getServiceManager();

    $em->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, function ($e) use ($sm) {
        /**
         * this will be triggered only by controlers in
         * this module/namespace...
         */
         $this->doSomething();
    });
}

您可以在管理模块中添加此内容,只有此事件才会被侦听此模块/命名空间内的类。

答案 1 :(得分:0)

您应该使用import pypyodbc connection = pypyodbc.connect('Driver={SQL Server};' 'Server=12.3.456.789.0123;' 'Database=mydb;' 'uid=BenchmarkingUS;pwd=mypwd') cursor= connection.cursor() data = [{'p1': "I'm hungry", 'p2': "text for col2", 'p3': 1234}] for dd in data: insert_sql = "INSERT INTO phyo_test (col1,col2,col3) VALUES (?, ?, ?)" cursor.execute(insert_sql, (dd['p1'], dd['p2'], dd['p3'])) cursor.commit() 对象!

如果所有控制器都在单个命名空间下,例如MvcEvent

\Admin\Controller