如何组织的国家/国家/城市浏览的MVC [英] How to organize country/state/city browsing in MVC

查看:113
本文介绍了如何组织的国家/国家/城市浏览的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个基于AJAX的网站,其中包括一个国家/国家/城市浏览界面。对于搜索引擎优化的目的,我想包括国家/国家/城市的URL的名称。例如,URL可能包括额外的变量,会是这样的:

I'm creating an ajax-based website that includes a country/state/city browsing interface. For SEO purposes, I want to include the name of the country/state/city in the URL. For example, the url may include additional variables, and would be something like:

 http://www.website.com/browse#!USA/NY/Albany
 http://www.website.com/browse#!USA/NY/Albany?Category=sports

我需要通过AJAX来适应这个功能集成到一个MVC框架(特别是codeIgniter)。我不知道如何组织的主要组成部分,特别是控制器,AJAX处理程序,以及可能的图书馆级。有没有一个标准或最佳实践方法?

I need to fit this functionality into an MVC framework (specifically CodeIgniter) through AJAX. I'm not sure how to organize the major components, specifically the Controller, AJAX Handlers, and possible Library class. Is there a standard or best practice approach?

推荐答案

是的,谷歌写了一些好东西在这个问题上,特别是保持与片段标识符的Ajax应用程序的状态: HTTP://$c$c.google.com/web/ajaxcrawling /docs/specification.html

Yes, Google wrote some good stuff on this subject, particularly maintaining ajax application state with fragment identifiers: http://code.google.com/web/ajaxcrawling/docs/specification.html

下面是一些code我把我的index.php文件来处理这个问题:

Here's some code I put in my index.php file to handle this:

// Special handler for ajax partials ("AHAH" paradigm)
if (isset($_GET['_escaped_fragment']))
{
    // Grab just the fragment
    $fragment = $_GET['_escaped_fragment'];

    // See if we have GET parameters to extract from the fragment
    if (FALSE !== ($p = strpos($fragment, '?')))
    {
        // Make sure to save additional GET parameters
        $additional_params = array_diff_key($_GET, array('_escaped_fragment' => 1));

        // Parse the querty string
        parse_str(substr($fragment, $p+1), $_GET);

        // Add the originals back in
        $_GET = array_merge($_GET, $additional_params);

        // The beginning part of the fragment is the base URI
        $fragment = substr($fragment, 0, $p);
    }

    // Otherwise, clear the $_GET array
    else
    {
        $_GET = array();
    }

    $_SERVER['PATH_INFO']   = $fragment;
    $_SERVER['REQUEST_URI'] = $fragment;

    // Add a constant for use throughout the app
    define('IS_FRAGMENT', 1);
}
defined('IS_FRAGMENT') OR define('IS_FRAGMENT', 0);

的做法是这样的:

1),如果它是一个Ajax请求,并IS_FRAGMENT == 1,则显示只需要填充前端变化的部分结果

1) If it's an ajax request and IS_FRAGMENT == 1, then display just the partial results you need to populate your front-end changes

2),否则,假设它是任何一个搜索引擎或直接页面加载。显示完全建立网页。

2) Otherwise, assume it's either a search engine or a direct page load. Display the fully built page.

您不一定要创建单独的终端只是为Ajax请求。这也可能是更好的扩展输出类,以提供所要求的格式的响应。它更多的RESTful的方式。

You shouldn't necessarily created separate endpoints just for the ajax requests. It's probably better to extend the Output class to provide a response in the requested format. It's more RESTful that way.

这也是很好的做法,利用HTML5历史API的浏览器可以处理它,并回落至onhashchange事件的其他人。

It's also good practice to leverage the HTML5 History API for browsers that can handle it, and fall back to the onhashchange event for the others.

多几个指针:

使用#!指示阿贾克斯触发的内容变化,而不仅仅是一个#。你可能会在某个时候需要使用默认的锚功能的片段提供(但感叹号是安全,你可能不会有以该特定字符元素的ID)。

Use "#!" to indicate ajax-triggered content changes, rather than just a "#". You might at some point want to use the default anchor functionality that the fragment provides (but the exclamation point is "safe" as you probably won't have element IDs starting with that particular character).

我也建议使用绝对URI(如#!/美国/纽约/奥尔巴尼),因为它会更容易留在你的AJAX部分加载机制是一致的。

I would also recommend using an absolute URI (i.e. #!/USA/NY/Albany), as it will be easier to stay consistent in your ajax partial loading mechanism.

此外,如果有人重新加载与ajaxed片段在URL的网页,它可以得到pretty的混乱,如果你不只是做一个window.location.href ='...'加载页面新鲜。你会碰到各种各样的与前进/后退,在这一点上的问题。

Additionally, if someone reloads the page with the ajaxed-fragment in the URL, it can get pretty messy if you don't just do a window.location.href = '...' to load the page fresh. You'll run into all kinds of issues with back/forward at that point.

这篇关于如何组织的国家/国家/城市浏览的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆