Use drupal_goto with named anchor in url - url

i am trying to go to an location choosen by my script.
I'm using url() and drupal_goto() to archive this, but the fragment-option named at the url()-documentation-page seems not working like i understand it or more likely drupal_goto() is changing the link.
The link-string i want should look like:
/topsection/section#subsection
but instead i'm getting the hash-sign encoded like
/topsection/section%23subsection
Here is my code:
$section = url( '/topsection/' . 'section', array( 'fragment' => 'subsection', 'alias' => TRUE ) );
drupal_goto( $section );
Any help would be nice!
Thank you.

Ha! just found the solution:
I misunderstood the documentation.
It is correctly telling that i should use drupal_goto() with fragment/anchor passed as option like i would give to url().
This is working:
drupal_goto( '/topsection/' . 'section',
array(
'fragment' => 'subsection',
'alias' => TRUE ) );

Related

Cant get requested GET param in yii

im trying create url like this: www.domain.com/auth?join=social in yii.
This is my config:
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'<action:(auth)>' => 'site/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
'showScriptName'=>false,
),
when i calling this: www.domain.com/auth?join=social page i cant get param: join. But when i calling www.domain.com/index.php/auth?join=social i can get.
im getting param with this code: $social = Yii::app()->request->getParam('join');. Where is my error?
$social = Yii::app()->request->getParam('social');
change it with
$social = Yii::app()->request->getParam('join');
I find my error it wasnt error YII. I must be write rewrite rules in lighttpd configurations. Here explained very well. Thanks!
try
$_REQUEST['join']
in your action.

How to handle GET params within urlManager's rule in Yii?

I pass a query string to SearchController::actionDefault in form of GET parameter q:
/search/?q=...
However I need to define a rule that would automatically initialize this parameter with some value or define another param.
If I'll request mysite.com/showall I need get the same content like in /search/?q=*
This is what I've tried:
'/showall' => '/search/default/index/?r=*',
I solved this!
there is possible to set defaultParams in urlManager, and finaly it looks like in application config file:
...
'components' => array(
...
'urlManager' => array(
...
'rules' => array(
....
'/show_all' => array( '/search/default/index', 'defaultParams' => array('show_all'=>'-') ),
....
),
...
),
...
),
The accepted answer also works when you are getting different requests and you need to map it to the same GET param.
For example I want all of these requests:
user/pics
user/photos
user/pictures
to actually generate: user/index?content=photos.
This might be one of a way to go:
'<controller:user>/(<content:photos>|pics|pictures)' => array('<controller>/index', 'defaultParams'=>array('content'=>'photos')),

ZF2 Route with Colon Separator

I am working with ZF2 and trying to setup Route configuration that uses a colon separator.
For example, the web address could be www.example.com/namespace:subject and I want to send it to a specific controller, action with the two variables. I am trying to use a Regex since the colon ":" is a special character for segments. Is there a nice way to do this? Here is my route configuration:
'dataReqs' => array(
'type' => 'regex',
'options' => array(
'regex' => '/(?<namespace>[^:]+).(?<subject>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'Application\Controller\Data',
'action' => 'get',
),
'spec' => '/%namespace%:%subject%',
),
),
EDIT: I want to use the colon as the prefix:resource format is commonly used in RDF syntax (http://www.w3.org/TR/2007/PR-rdf-sparql-query-20071112/#QSynIRI). For instance, a long uri like http://dbpedia.org/data/Semantic_Web with a #prefix dbp: http://dbpedia.org/resource/ may be referred in a document with dbp:Semantic_Web. So for my Linked Data server I could direct requests and include the prefix (namespace) and the resource name; eg http://myserver.com/dbp:Semantic_Web. While I am using the segment combinations /namespace/resource for now, it would be nice to handle a route with prefix:resource syntax.
Do not use colon in your route. It isn't good practice, because colon is reserved character(see https://www.rfc-editor.org/rfc/rfc3986#section-2.2)
I'm inclined to agree with kormik. Why do you want to specify URL's in that way? What is wrong with the default behavior?
www.example.com/namespace/subject
eg:
www.example.com/somenamespace/10
or even:
www.exmple.com/namespace/namespace/subject/subject
eg
www.example.com/namespace/somenamespace/subject/10
you can easily grab these parameters in the controller like so:
$ns = $this->params()->fromRoute('namespace',0);
$subject = (int) $this->params->fromRoute('subject',0);
You would need to modify the route config also.

How to put translator in Zend2 form

Iam new guy to Zend framework and currently Iam working on Zend2...I want to ask about Translator usage in Zend forms....If i want to use translator i directly using for labels in form view i.e.form_view.php like
$this->formLabel()->setTranslator($translator, 'date_of_birth');
But I want to add the translator at the form only i.e.in src/my_module/Form/UserForm.php
like
$this->add(array(
'name' => 'date_of_birth',
'attributes' => array(
'type' => 'text',
'id' => 'date_of_birth',
),
'options' => array(
'label' => 'DateOfBirth',
), //Here there is any option to put translator
));
Please help me...any answer would be help for me like I asked
Thanks in advance
You don't really need to do that. Since the the Translator that is set up using the factory-key translator will automatically be injected into the Form.
The best approach (in my opinion) is to make extensive use of the translator text_domain:
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
'text_domain' => 'MyModuleTextDomain'
),
),
),
With this setup, the Files of your Module will automatically be inserted into the default TranslatorService which every Zend\Form knows of.
So ultimately all you have to do is make the ViewHelpers know of the TextDomain that you are using. And this is done in the following manner:
$this->formLabel()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formButton()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formElementErrors()->setTranslatorTextDomain('MyModuleTextDomain');
You need to do this once inside your respective view.phtml before(!) using the ViewHelpers like $this->formElement($element) or $this->formCollection($form)
And that's really all there is to it. I recall having seen a discussion somewhere about making it easier to pass along Text-Domain-Data, but i can't find it right now. So things may get a little easier in the future ;) For now, 3 lines are all that's needed though!
above answer is quite unnecessary ... as your translator was added automatically to zend form for rendering form labels and ....
only use this code in your module config :
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
),
),
),
if u use the correct view helpers for rendering form elements (or whole form) it will automatically translated
This is not a recommended approach because forms are translated automatically if you have a translator configured (which you do if you are using the Skeleton Application). However, since you asked how to use the translator directly within your form, I will show you how you can do it. Please carefully consider if you really want to do this, as I cannot imagine a use case where it would be necessary.
To do exactly what you were asking, you can inject the translator into your form. You can do this either in your controller or in a factory. I will be using a factory in this example because it is more DRY.
// In your module's config file
'service_manager' => array(
'factories' => array(
'YourModule\Form\YourForm' => function($sm) {
$translator = $sm->get('Translator');
return new \YourModule\Form\YourForm($translator);
},
),
),
Then in your form class, you can do like this:
namespace YourModule\Form;
class RegisterForm extends \Zend\Form\Form {
public function __construct($translator) {
// Do something
$translated_string = $translator->translate('string to translate');
}
}
Then in your controller, you can do like this:
$your_form = $this->servicelocator->get('YourModule\Form\YourForm');
Or if you don't want to use the factory, you can choose to not add it and do like this instead:
$your_form = new \YourModule\Form\YourForm($this->servicelocator->get('Translator'));
I would recommend going with the factory, though.

Change default controller path in lithium

I'm writing a new version of an API and would like to support legacy versions by having distinct sets of controllers for each version. Within the default "app\controllers" path in Lithium, I would like to have for example "v1" and "v2" paths.
I have tried accomplishing this within the route itself by doing something like:
Router::connect('/{:version}/{:controller}/{:action}{:args}', array(
'controller'=> '\app\controllers\{:version}\{:controller}Controller',
), array());
Then I tried overriding the path in the libraries bootstrap module by doing something like:
if( preg_match('/^\/(v[0-9\.]+)/', $_SERVER['REQUEST_URI'], $match) ) {
Libraries::paths(array(
'controllers' => "controllers\\".$match[1].'\\{:name}Controller',
'models' => "models\\".$match[1]."\\{:name}",
));
}
I spent about a half a day at work searching google and the very sparse lithium docs. I am not sure what release of Lithium we are using as I have stepped into this pre-existing code base.
Thanks for any tips you may have!
In your routes.php file, you should re-configure the Dispatcher default rules with
Dispatcher::config(array('rules' => array(
'v1' => array('controller' => 'app\controllers\v1\{:controller}Controller')
)));
and a continuation route to match /v1/... requests
Router::connect('/v1/{:args}', array('v1' => true), array(
'continue' => true, 'persist' => array('controller', 'v1')
));
You can easily use :version instead of a predefined version number if you need so.

Resources