Rails 3: mixing "generic" controller routes with standard resource routes - ruby-on-rails-3

I've got a set of routes that looks like this:
resources :placements do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
This produces routes that behave like you would expect:
/placements/1234/foo
/placements/1234/bar
However, I also need "generic" routes for a few methods that do not need an individual placement. So, I build a routes block that looks like this:
resources :placements do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
match '/placements/baz' => "placements#baz"
If I rake routes, I get a route that looks good:
/placements/baz
Note the lack of an id. However, if I try to visit that route, Rails tries to call the show method on the controller instead, as if "baz" was an ID, instead of a method name. How can I build a routing structure that gives me what I am after, without having to change the first segment of my route (placements), to something else?

Move the second route over the resources block ie,
match '/placements/baz' => "placements#baz"
resources :placements, :id => /\d+/ do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
or add a regex for the id in resources, ie something like:
resources :placements, :id => /\d+/ do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
match '/placements/baz' => "placements#baz"

Related

YII2 UrlManager wrong route

On my site I have this urlManager rule:
'city/<id:\d+>-<alias:\S*>' => 'city/view',
On page of module "user", for example this https://example.com/user/profile, there is a link for rule
Url::to(['city/view', 'id' => $this->id, 'alias' => $this->alias], $absolute)
But link becomes this https://example.com/user/city/view?id=1&alias=city_alias
What I am doing wrong?
You need to add module ID in the route as well. In the simplest case it's
'user/city/<id:\d+>-<alias:\S*>' => 'user/city/view'
If there are more than one module using similar route you can use wildcard
'<module>/city/<id:\d+>-<alias:\S*>' => '<module>/city/view'
You should simply try :
Url::to(['/city/view', 'id' => $this->id, 'alias' => $this->alias], $absolute)
Read more about creating urls.

Yii The view file does not exist - where do mail layouts go

I am using basic template but have adapted the advanced user password reset functionality, for some reason I can't get it to find the mail layouts.
So in \mail\layouts I have
- passwordResetToken-html.php
- passwordResteToken-text.php
In web.php I have
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => 'app\mail\layouts',
...
The advanced template uses
'viewPath' => '#common/mail',
But as i'm using basic template, its not in the common/mail folder.
In sendMail function in PasswordResetRequestForm.php i have
return \Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])
...
However getting error
The view file does not exist: app\mail\layouts\passwordResetToken-html.php
I know this is going to be something small but for the life of me i cannot see it
Removed the 'viewPath' of config, as the doc don't use it.
So it works.

Routing not working in phalcon framework

I have many modules and depending upon modules routes are generated dynamically. For explaining purpose I m adding route for just one module. Following are the routes for the users module.
<?php
//1
$router->add('/users',array(
'module' =>'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index'
));
//2
$router->add('/users/:params/',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index',
'params'=>1
));
//3
$router->add('/users/:action',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1
));
//4
$router->add('/users/:action/:params',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1,
'params'=>2
));
?>
Lets say the url for the user module is
http://www.example.com/admin/users/
This url matches the very first route and its working as expected. But when we navigate to next page, my url looks like
http://www.example.com/admin/users/2
now the problem is it should match 2nd route, but it matches 4th route. If I move 2nd route all the way down, the above url work, but the url
http://www.example.com/admin/users/search/1 will not work
Can anybody help me make it work?
Thanks
:params is tricky because it will match anything. So /users/:params/ matches both /users/:action and /users/:action/:params, which itself has :params in it - making it a routing mind blow.
As a general rule avoid the mind blow scenario. For example you can put :params at the end of the longest possible match (/users/:action/:params) and then rewrite the shorter routes without any :params in them.

Middleman and I18n: having some issues

I have activated I18n in middleman like so:
activate :i18n, mount_at_root: :de
Now I'd like to be redirected from / to /de automatically. Is this possible?
Also, I wonder why middleman auto-assigns class index (for german) and en_index (for english) using the page_classes helper? This doesn't make much sense - it's the same page, so it should use the class index for both english and german. Or did I miss something?
If you :mount_at_root => :de german will be your default language and thus not prefixed.
If you set :mount_at_root => :false all languages should be prefixed.
I have successfully used the following configuration to set de/en paths.
This will also create page_classes such as as en en_index and de de_index.
activate :i18n, :mount_at_root => :false, :langs => [:de, :en]
http://middlemanapp.com/advanced/localization/
Redirecting from / to /de is done using redirect "index.html", :to => "de/index.html".
To prevent page_classes from prefixing the classes with the language, overwrite the helper like so:
helpers do
def page_classes(path=current_path.dup, options={})
super(path.sub(/^[a-z]{2}\//, ''), options)
end
end

Zend Framework - how to rewrite url to seo friendly url

I got website on Zend Framework (im total noob in Zend). For example I would like to make one URL "somewebsite.com/test/about" to look like this "somewebsite.com/for-fun-link". How do i achieve this in Zend ? Im newbie in Zend and Apache server. Where i do rewrites in Zend ? I want static URL somewebsite.com/page/about rewrite to somewebsite.com/about-product
And last question: where do i usually create rewrites ? its depends of sever/technology ?
In your bootstrap, you'll need to configure some "routes". So, if your bootstrap ends something like this:
$frontController = Zend_Controller_Front::getInstance();
$frontController->dispatch();
you can just add in some route definitions like this:
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute( 'mylovelyroute',
new Zend_Controller_Router_Route_Static( 'for-fun-link',
array( 'controller' => 'test', 'action' => 'about' )
)
);
$router->addRoute( 'myotherroute',
new Zend_Controller_Router_Route_Static( 'about-product',
array( 'controller' => 'page', 'action' => 'about' )
)
);
$router->addRoute( 'justonemore',
new Zend_Controller_Router_Route_Static( 'another/longer/path',
array( 'controller' => 'mycontroller',
'action' => 'myaction',
'someparameter' => 'foo'
)
)
);
$frontController->dispatch();
The first parameter of addRoute() is just a unique name. Zend_Controller_Router_Route_Static takes the path you'd like to capture, and then an array of parameters, including a controller and action (and module if it applies).
If you wanted to add complexity, you could load the routes out of a database, or start using more dynamic routes. The docs here are a useful next step: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard