I have a url that loads fonts as such:
/templatename/fonts/helvetica?weights=regular,bold,light
in php it will dynamically generate a CSS file with the appropriate font referencing.
We just recently moved over to to Phalcon and it broke. I'm trying to figure out how to tell the router to use the the font name as a named param but also use the standard param style. with the question marks.
this is what my router looks like right now:
...
"fonts"=>[
"pattern" => "/fonts/{file:[\w\W]}",
"route" => [
"controller" => "asset",
"action" => "fonts"
]
]
...
When I use the dispatcher loop, like this:
$params = $this->dispatcher->getParams()
The array does not show the weights param:
Array
(
[template] => templatename
[file] => helvetica
)
How can I get it to look like this without changing the URL structure?
Array
(
[template] => templatename
[file] => helvetica
[weights] => regular,bold,light
)
If you have the following URL:
/templatename/fonts/helvetica?weights=regular,bold,light
Then weights=regular,bold,light are the GET parameters.
You can request these inside Phalcon by using:
$weights = $this->request->getQuery('weights')
You do not have to declare this inside your routes, Phalcon automatically appends these GET parameters to the end of your routes.
Check the Phalcon HTTP Request docs for more info
Related
I am new to Yii2. I am getting used to it slowly. I am using a module to build customer data. I have a customer table. My module name is Customer. My controller name is Customer.
Finally my url structure after enabling pretty url is as below.
http://localhost/basic/web/customers/customers
http://localhost/basic/web/customers/customers/create
etc
How do i remove the module name(customer) from the url?
I want to make something like this.
http://localhost/basic/web/customers/
http://localhost/basic/web/customers/create
My folder structure
Modules
----Customers
------controllers
-----------DefaultController
-----------CustomersController
------Models
-----------Customers
------Views
-----------customers
--------------create
--------------update
...
----Module.php
I dont know how to insert rules in urlManager even after following this:
Yii - Hiding module name in URL
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<action:\w+>'=>'module/controller/action',
),
You have all you need is this and take care, you are looking a Yii1 question.
If your module name is called like your controller try that:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' =>[
'<controller:\w+>/<action:\w+>'=>'controller/controller/action',
],
We want the format to be used in the Url is controller/action.
With <controller:\w+> we "declare" a "variable" called controller with cointains words, and the same with <action:\w+>.
The internally Yii will call <controller>/<controller>/<action>
Remember controller and action are "variables"
If you want call always the same module you will use:
'rules' =>[
'<controller:\w+>/<action:\w+>'=>'customers/controller/action',
],
I'm trying to remove the action in the cakephp url and add a slug inflector, to be more clear this is my expected output:
from this: example.com/posts/view/81/This is a test post
to this: example.com/posts/This-is-a-test-post
This is my current code:
That gives me this output: example.com/posts/view/This is a test post
Controller:
public function view($title = null) {
if (!$title) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findByTitle($title);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
view.ctp:
$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['title']));
I also tried this one,this link being my reference CakePHP: Use post title as the slug for view method :
Output: example.com/posts/view/21/This-is-a-test-post
Controller:
function view($id) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
view.ctp
$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id'], Inflector::slug($post['Post']['title'],'-')));
below are the other links that I tried but failed, I also tried the modifying the routes.php but can't make the proper code to make it work
want to remove action name from url CakePHP
How to remove action name from url in cakephp?
Remove action name from Url in cakephp?
any help/suggestions is appreciated thanks.
Use Id and Named parameter...
On routes.php
define new routes as-
Router::connect('/posts/:id-:title',
array('controller' => 'posts',
'action' => 'view')
);
This new defined routes will match and parse all url containing id and title named parameter..
Important Note:: Do not define new routes after the end of routes.php. Try to define on the middle of the file..
On view
echo $this->Html->link('Hi',array(
'controller' => 'posts',
'action' => 'view',
'id' => 4,
'title' => Inflector::slug('the quick brown fox')
));
Well the solution provided above will fulfill your needs, but still after reading your question one thing comes into my mind... in your controller you are trying to read posts using title I mean this will slow down your system not recommended in programming so use id and increase one more column in your db table for slug(SEO title) which will be used for creating your post urls.
For example:
Post title: This is a test post
Create seo title for this as: this-is-a-test-post-123
Stor seo title in DB as <this-is-a-test-post>
See 123 is your posts ID now change your controller function to get data based on id ie.123, I hope you cn extract 123 from the sting easily...
Note: remember you have to think about this-is-a-123 string also because
they also land in post having id 123.
Use below route for the above solution:
Router::connect('/posts/*', array('controller' => 'posts', 'action' => 'view'),array('pass' => array('title')));
Now in your controller:
You will get string "this-is-a-test-post-123" in $post_title
function view($post_title=null){
$temp = explode('-',$post_title);
$posts_id= end($temp);
$lastKey = end(array_keys($temp));
unset($temp[$lastKey]);
$seoTitle = implode("-",$temp);
//Now compare the above seoTitle with DB seo title for unique urls
}
I'm trying to pass an audio URL as a parameter in Ruby on Rails, and googling "how to pass a url in a url" has proved to be problematic. The problem is that I can only get the url to return minus a crucial forward slash.
My route looks like:
get 'my_page/:title/*url', to: 'my_page', action: 'show'
The variable is defined in the controller like so:
#url=params[:url]
So the request would look like:
www.my_app.com/my_page/im_a_title/http://im_a_url.mp3
The url variable, however, ultimately results missing a slash after the prefix:
http:/im_a_url.mp3 <--notice the missing forward slash
Of note, the construction of the urls varies enough that it would be cumbersome to construct them. (Some begin with http and some https, for instance.) How do I preserve the syntax of my url? Or is there a better way to pass this parameter all together?
Why don't you pass the url as a required parameter. That way you can use the built-in to_query.
get 'files/:title' => 'files#show'
and in files controller
class FilesController < ApplicationController
def show
url = params.fetch(:url) # throws error if no url
end
end
You can encode the url and unencode like so:
{ url: 'http://im_a_url.mp3' }.to_query
# => "url=http%3A%2F%2Fim_a_url.mp3"
Rack::Utils.parse_query "url=http%3A%2F%2Fim_a_url.mp3"
# => {"url"=>"http://im_a_url.mp3"}
You should url encode the parameters before passing them in
Rails 4
match 'my_page/:title/:url', to: 'my_page#show' , constraints: { url: /[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}/}, via: :get, :format => false
constraints will match only a regex for a url.
format => false will ignore the dot part of the url
How to hide/encrypt everything(id) in the URL in the browser except the site name and controller name?
I think UrlManager can do it, but I don't know how ? need url mapping similar in ROR
my url manager code
'urlManager'=>array(
//'urlFormat'=>'path',
'showScriptName'=> false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
I like to add a random number between every action(for secure my urls)
ROR eg:
map.connect 'by/:develop_name',
:controller => 'developer',
:action => 'builder_projects'
Please explain step by step.
couple if links I found relate to this
LINK1
LINk2
You just need to specify your application routes appropriately. Before continuing, you should read the URL management chapter of the Yii guide.
What you want to do is use named parameters in your rules, which means that the rule definition would look like this:
'by/<id:\w+>' => 'developer/builder_projects'
This rule takes a URL of the form http://site.com/index.php/by/42 and routes it to the controller developer, action builder_projects with the parameter id equal to whatever 42 (this is what the regular expression \w+ matches).
Routes are specified in your application configuration file as parameters to the urlManager component:
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'by/<id:\w+>' => 'developer/builder_projects'
// more rules
),
),
What you could do is define a helper function which symmetrically encrypts/decrypts:
class Helper {
public static function myCrypt($data, $decrypt = false){
//Logic to encrypt/decrypt
return $result;
}
}
and then when you create urls you can do:
$this->createUrl("myRoute", array("secret_id" => Helper::myCrypt($secret_id)));
and then in the controller action this resolves to you can do this:
public function actionMyRoute($secret_id){
$secret_id = Helper::myCrypt($secret_id, true);
//Do what you need to do with the decrypted id
}
Just make sure your encryption method returns a url safe string.
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.