Giter VIP home page Giter VIP logo

Comments (15)

rottekruid avatar rottekruid commented on May 29, 2024

(edit) Sorry the formatting was messed up. Will post a new response.

from yaf.

rottekruid avatar rottekruid commented on May 29, 2024

Hello,

My solution is quick and is not perfect and not very elegant, but it might be a
starting point.

If you have something better, please reply with your better solution :)

Maybe we should start a YAF code-repository for commonly-needed functions?

First, define constant BASE_URL (or similar):

define('BASE_URL', 'http://www.example.com');

Secondly, extend the controller. I like to have a "base" controller for every
application:

/**
    * @name BaseController
    * @desc A base controller for the application
    * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
    */

class BaseController extends Yaf_Controller_Abstract {
        // ...
}

Thirdly, add method to the BaseController class:

/**
* URL generating helper intended to generating valid internal hyperlinks.
* Important: It requires that a constant named BASE_URL be defined.
*
* @param string $actionName Optional action name (defaults to current action)
* @param string $controllerName Optional controller name (defaults to current controller)
* @param string $moduleName Optional module name (defaults to current module)
* @param array $params Optional controller parameters
* @param array $getParams Optional query parameters
* @param string $target Optional target
* @return string The generated URL
*/

public function url($actionName = NULL, $controllerName = NULL, $moduleName = NULL, array $params = array(), array $getParams = array(), $target = NULL) {

    // Build URL using parts

    $urlParts = array();

    $urlParts[] = BASE_URL;

    if ($moduleName === NULL) $moduleName = $this->getRequest()->getModuleName();
    if ($controllerName === NULL) $controllerName = $this->getRequest()->getControllerName();
    if ($actionName === NULL) $actionName = $this->getRequest()->getActionName();

    $moduleName = strtolower($moduleName);
    $controllerName = strtolower($controllerName);
    $actionName = strtolower($actionName);

    // Get default module, controller and action names

    $Application = Yaf_Application::app();
    $Config = $Application->getConfig();

    $Application->getConfig()->application->dispatcher->defaultModule ? $defaultModule = $Application->getConfig()->application->dispatcher->defaultModule : $defaultModule = 'Index';
    $Application->getConfig()->application->dispatcher->defaultController ? $defaultController = $Application->getConfig()->application->dispatcher->defaultController : $defaultController = 'Index';
    $Application->getConfig()->application->dispatcher->defaultAction ? $defaultAction = $Application->getConfig()->application->dispatcher->defaultAction : $defaultAction = 'index';

    $defaultModule = strtolower($defaultModule);
    $defaultController = strtolower($defaultController);
    $defaultAction = strtolower($defaultAction);

    // Assign module name

    if ($moduleName != $defaultModule) {
        $urlParts[] = strtolower(trim($moduleName, '/')); // To validate a module, inspect its presence in $modules = $Application->getModules();
    }

    // Assign controller name

    if ($actionName != $defaultAction || $controllerName != $defaultController || $moduleName != $defaultModule) {
        $urlParts[] = strtolower(trim($controllerName, '/'));
    }

    // Assign action name

    if ($actionName != $defaultAction) {
        $urlParts[] = strtolower(trim($actionName, '/'));
    }

    // Assign parameters (assumes url parameter pairing)

    foreach ($params as $k => $v) {
        if ($v !== NULL) {
            $urlParts[] = $k;
            $urlParts[] = $v;
        }
    }

    // Assign get parameters

    $getParamsStr = '';
    foreach ($getParams as $k => $v) {
        if (!$getParamsStr) {
            $getParamsStr = '?';
        } else {
            $getParamsStr .= '&';
        }
        $getParamsStr .= rawurlencode($k) . '='. rawurlencode($v);
    }
    if ($getParamsStr) {
        $urlParts[] = $getParamsStr;
    }

    // Build the URL

    $url = implode('/', $urlParts);

    // Assign # target

    if ($target !== NULL) {
        $urlParts = explode('#', $url);
        $url = array_shift($urlParts) . '#' . rawurlencode((string) $target);
    }

    return $url;

}

Assign the controller to the view, if you want to, something like this in your
action:

$this->getView()->_controller = $this;

The it's quite simple to generate internal application URL's in the view:

<a href="<?=$_controller->url('myaction', NULL, NULL, array(),
$_GET)?>">Some link</a>
<a href="<?=$_controller->url('myaction')?>">Some link</a>
<a href="<?=$_controller->url('myaction', 'othercontroller')?>">Some link</a>

Hope it provides some guideline?

from yaf.

rottekruid avatar rottekruid commented on May 29, 2024

Also, a nice feature of YAF is to prefer action names when interpreting URL's

In your .htaccess, add:

# If there is only one part in PATH_INFO, should it consider as a controller
# or action. If action_prefer is configured On, it will be considered as a
# Action name.
php_value yaf.action_prefer Off

from yaf.

openalmeida avatar openalmeida commented on May 29, 2024

Whats a quickly reply!
Thanks very much.

Yes, url generating helper is really want i've said/want,
actually that all of these:

Yaf_Route_Static
Yaf_Route_Simple
Yaf_Route_Supervar
Yaf_Route_Map
Yaf_Route_Rewrite
Yaf_Route_Regex

or more customed Yaf_Route_* classes need it to be suit.
but, url generating helper point a new way for me.

Thanks again. Thanks very much. Especially for your quickly reply.

from yaf.

 avatar commented on May 29, 2024

If your problem was resolved, please leave this ticket closed.

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

有的..assemble,可以通过路由给出url, 文档目前不全..

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

可以先看下 47e528e 中的测试文件

from yaf.

laruence avatar laruence commented on May 29, 2024

@zxcvdavid 你也知道文档不全? 文档不全你还在不去赶紧.......

from yaf.

wenjun1055 avatar wenjun1055 commented on May 29, 2024

至少半年了吧。。。。。。。。。。。。。

from yaf.

 avatar commented on May 29, 2024

是不是成立个Ya Foundation,专事项目开发,文档?现在太松散了,没有组织,没有纪律。

from yaf.

 avatar commented on May 29, 2024

我说不定可以贡献一点点力量。有一定的空余时间。

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

@netroby 欢迎贡献

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

文档周五已经提交,等 @laruence 发布到php.net就可以看到了

from yaf.

openalmeida avatar openalmeida commented on May 29, 2024

Ok, lets end.

from yaf.

shyandsy avatar shyandsy commented on May 29, 2024

@zxcvdavid 所以,到底如何在controller中使用assemble生成url啊

from yaf.

Related Issues (20)

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.