Comments (15)
(edit) Sorry the formatting was messed up. Will post a new response.
from yaf.
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.
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.
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.
If your problem was resolved, please leave this ticket closed.
from yaf.
有的..assemble,可以通过路由给出url, 文档目前不全..
from yaf.
可以先看下 47e528e 中的测试文件
from yaf.
@zxcvdavid 你也知道文档不全? 文档不全你还在不去赶紧.......
from yaf.
至少半年了吧。。。。。。。。。。。。。
from yaf.
是不是成立个Ya Foundation,专事项目开发,文档?现在太松散了,没有组织,没有纪律。
from yaf.
我说不定可以贡献一点点力量。有一定的空余时间。
from yaf.
@netroby 欢迎贡献
from yaf.
文档周五已经提交,等 @laruence 发布到php.net就可以看到了
from yaf.
Ok, lets end.
from yaf.
@zxcvdavid 所以,到底如何在controller中使用assemble生成url啊
from yaf.
Related Issues (20)
- Request::setControllerName 第二个参数format==false时 设置失败 HOT 1
- Yaf_Registry::set 的第二个参数为什么注释为 string 类型 HOT 1
- yaf model 的问题 HOT 2
- 开启公用类库后,无法使用composer HOT 2
- 偶发性问题Uncaught Error: Call to a member function run() on bool in ....../public/index.php HOT 1
- 希望鸟哥能增加一个配置用于控制是否开启默认路由.
- yaf routing with namespace is on option HOT 4
- Fatal error: Class 'Yaf' not found HOT 2
- PHP8.2下 安装yaf-3.3.5报错 HOT 5
- Yaf_Config_Ini 造成 Too many open files 问题 HOT 3
- 使用了__get魔术方法后,当action不存在的时候,php进程会被直接终止,无法捕获错误。 HOT 4
- 支持phpstorm的代码查找
- v3.3.5 Yaf_View_Simple __isset bug
- fpm进程中存在多个application.ini文件句柄 HOT 2
- Error: Failed opening controller script
- PHP8.3 install yaf-3.3.5、yaf-3.3.4 extenstion error:make: *** [Makefile:209: yaf.lo] Error 1 HOT 2
- README.md contains outdated references to Travis CI service
- php-ext
- 使用 Yaf_Request_Http 对象的 getRequest(), getPost(), getQuery() 3个方法获取单个参数时候会导致程序中断
- 能否写个php 多线程处理的扩展啊。现在并发需求多。用户访问量大。
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yaf.