Giter VIP home page Giter VIP logo

Comments (2)

visamz avatar visamz commented on August 11, 2024

挖掘到一个功能,发现路由可以介样写:

return array(
    '/' => array(
        'action' => 'Quotation\Action\Index', 
        'params' => array('routeName' => 'index')
    ),
    '/s' => array(
        'action' => 'Quotation\Action\Search',
        'params' => array('routeName' => 'search')
    ),
    '/brands' => array(
        'action' => 'Quotation\Action\Brands',
        'params' => array('routeName' => 'brands')
    )
);

然后,利用 params 这个参数,就可以根据 routeName 获取对应的 url:

/**
     * toUrl
     * @return mixed
     */
    public static function toUrl()
    {
        if (!self::$format) {
            $routes = self::get('routes', ''); // 先取得路由配置,这个是我自己写的方法。

            foreach ($routes as $k => $v) {
                self::$format[$v['params']['routeName']] = asset(preg_replace('/:([_a-z]+)/i', '%s', $k));//asset 也是我自己的方法,用来组建 url 
            }
        }

        $args = func_get_args();
        if (count($args) > 0) {
            $name = $args[0];
            $args = array_slice($args, 1);
            return vsprintf(self::$format[$name], $args);
        }

        return self::$format;
    }

from framework.

visamz avatar visamz commented on August 11, 2024

当然了,除此之外,你的action里相应也可以利用 params参数。
在你指定了 params 后,比如 'params' => array('routeName' => 'index')
action 里也会自动搜索是否存在 setRourteName 这个方法,如果存在,将自动执行。
执行顺序如下:init(),你定义的 setRourteName方法,最后是execute()

最后,还有一个好玩,除了定义'params'外,还有一个参数可选,那就是拦截器,interceptors,慢慢折腾吧...

from framework.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.