CakePHP and plugins in URLs - url

I'm using a plugin in a CakePHP app together with some (admin) views in it. The URLs inside the plugin views use a structure like:
$html->url(array('plugin' => 'thePlugin', 'controller' => 'theController', ...));
When using other URLs on the same page (e.g. in the layouts file), CakePHP attaches this plugin parameter to every URL, except the plugin parameter is set to null:
$html->url(array('plugin' => null, 'controller' => 'otherController', ...));
That's the "magic" of CakePHP. Nice!
But is there a way to disable the plugin parameter or set the default value to null unless the parameter is explicit set in $html->url()? It would save a lot of time to not rewrite each URL and add 'plugin' => null to disable this parameter.
Thanks for your hints!

cakephp 1.3 have this problem. you can't set router for each plugin. but in cakephp 2 you can set route for every plugin urls.

Related

Routing in Kohana without showing controller name

I want to create a routing in Kohana Framework Version 3.3.1.
I want URL like http://www.test.com/male/London and internally they will act like below URL-
http://www.test.com/list/search/London
I want to hide the controller and action name from the URL.
Any help is greatly appreciated.
This can be accomplished by using routes in bootstrap.php and/or a module's init.php file.
For example, you could set up a route for (male/<location>) and then your default controller would be list and the action search.
You could then access the location within the controller/action by using $this->request->param('location'); to be used in whatever DB query you need it.
Confused? Have a read through this section of Kohana Docs and it should all make sense.
You have to do two changes in your file:
Bootstarp file :
Route::set('list', 'male/<id>' )
->defaults(array(
'controller' => 'list',
'action' => 'search',
));`
And second is you can make your link like
href="<?php echo URL::site('male/'.id, TRUE) ?>">
And this route file should above at your default route file.

CakePHP 2.x: How can I overwrite an URL pointing to the index page?

I'm getting this issue:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
This will show www.mysite/controller/login as the site URL
I would like to overwrite www.mysite.com/controller/login with just www.mysite.com, but still go to the login page. Does anyone know how to do it with Cake 2.x?
The behavior It's not exactly as you describe.
What the following does:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
is allow you to type www.mysite.com in your browser, and get the view that www.mysite.com/controller/login renders.
It works like an url rewrite instead of a redirect. Therefore, the above should work as expected. However, if it's not an example, try to name your controller differently, as it may cause trouble with CakePHP.
As stated by Inigo Router::connect() just connects a route/URL to a controller action. So with your defined route you should be able to goto www.mysite.com and your login action will be served (although I'm not sure that it is a good idea to have the base URL act as the login page).
It does not prevent www.mysite.com/controller/login from working as this is one of CakePHP's default routes.
To disable the default routes you need to remove this line from routes.php:-
require CAKE . 'Config' . DS . 'routes.php';
Be warned, if you remove this line you must have defined routes for all your pages in your app's routes file. This is not necessarily a bad thing, Beware the Route to Evil is a good read in regards to this.
As I used the "Auth" component I had to add in the function
beforeFilter()
of my controller this line:
$this->Auth->allow('anAction', 'anotherAction', '**login**');

How to hide url in angular?

Is it possible to not using angualar routes, because if I defined an routes like:
app.config(function($routeProvider){
$routeProvider.when('/home')
})
my url will look like www.app.com/#home,
I dont want to change url, just www.app.com nice and clean, in this case how to defined template for defferent controller and how to load the template in ng-view, and how to pass url parameter?
any idea?
You cannot do that with the built-in routing mechanism because path is required for every route. However, you can use ui-router library in order to create URL-less states and navigate to them using either code or directive.
I created this plunker to demonstrate how to use ui-router to navigate among states without modifying URL.

Approach for AngularJS template translations with Rails server

I need to implement an application with multi-language support using an AngularJS front-end and a Ruby on Rails server.
I am looking for a reasonable approach to render translated templates in multiple languages. I have come up with an approach I would like feedback on.
In the Angular routes definitions, set the template property to an html partial that just has an ng-include with the src attribute value set by the controller. This approach is needed to dynamically modify the path to the template to be fetched from the server; it is described here:
AngularJS - How to use $routeParams in generating the templateUrl?
So the Angular route config would look like:
angular.module('myApp', []).
config(function ($routeProvider) {
$routeProvider.when('/sites/new', {
template: '<div ng-include src="templateUrl"></div>',
controller: 'RouteController'
});
});
And the controller would look like:
// Set a prefix on the URL path, something like “es”
function RouteController($scope, $routeParams) {
$scope.templateUrl = $routeParams.locale + "/template/sites/site";
}
Here $routeParams.locale is used to set the locale, but could be a variable set by user action. The approach for dynamically modifying the template URL path to add a locale prefix seems a bit convoluted, but I know of no other way.
On the Rails side, in routes.rb, add a route:
match '/:locale/template/*template' => 'template#get'
The route uses route globbing so the params[:template] value can be a multi-level path.
The TemplateController#get action just renders the partial determined by params[:template]
The Template controller code is something like:
class TemplateController < ApplicationController
layout false
caches_page :get
def get
render(template: "template/#{params[:template]}")
end
end
The Rails I18n support for translations is used in the erb templates, translating according to the locale parameter.
In production, caching would be turned on. This would avoid incurring translation overhead. The locale prefix of the URL path would result in a per language set of translated templates to be cached.
This approach pushes translations processing to the server side as much as possible.
Is there any fundamental problems with this approach?
Could it be done better?
You may be interested in the angular-translate module.
We achieved client side internationalization in AngularJS with the i18next JQuery plugin http://i18next.com/ and created a filter called i18n.
When you initialize the Angular Application, you initialize the i18n plugin where you can provide a pattern to locate the file containing labels, and in the template use this as an example for binding labels and values.
{{'mynamespace:labels.firstname' | i18n}}
where "mynamespace" is used to separate your labels logically and used to locate JSON files with the labels. Within json files you can have one or more JSON objects with labels as properties. In the above example, the file called mynamespace-i18n-en-US.js if you provided a pattern __ns-i18n-__lng__.js
Is there a reason why the translation has to happen on the server?
Do you really need to translate the entire template?

Generating Outbound Links To Main Website From Widget

I've got an ASP.NET MVC 3 web application that lives on server 1:
http://www.mysite.com
And i've got a widget (also ASP.NET MVC 3 web application) that lives on server 2:
http://widget.www.mysite.com
Now, i want to generate a link to the main website from my widget.
So if i do something like this in the widget project:
#Html.RouteLink("Outbound link", "MainWebsiteRoute", "http", "http://www.mysite.com", string.Empty, null)
It will error (or generate the wrong link) because the route tables are different between the widget and website project.
Even if i copy the route from the main website into the widget one, the controllers/actions don't exist in the widget project, so it won't work.
How do i do it? Do i have to hardcode the URL's?
Ended up duping the routes from the main website that i need in the widget.
Example route in main website:
context.MapRoute(
"Q&A - Individual Q",
"{locationUniqueUri}/questions/{questionUniqueUri}",
new { controller = "Questions", action = "Detail" },
);
Duped route in widget:
RouteTable.Routes.MapRoute(
"Outbound - Q&A - Individual Q",
"{locationUniqueUri}/questions/{questionUniqueUri}"
);
Notice how there are no route defaults - that's because i don't care, since that's concerning matching the route to a controller/action. But this happens in the website, not the widget. All i'm concerned about is generating the URL.
I then created a simple wrapper for RouteLink called OutboundRouteLink which specifies the hostname (for the main website), and generates the link.
I don't think it's too bad a solution. Obviously, it's hard-coded in a way - if the routes change in the main website then i'll have to change the widget routes. But that fine, because if one of my URL's changed, i'd have to do 301's anyway, so the old URL's would still work for the widget (they would get 301'd).
Also, i could go one step further and store the route url in a config file and share that between the projects, which would give me a bit more safety. But there's only around 3 outbound URL's so this should be fine.
Not saying this is the best way to handle it, but im happy with it.
Are you using areas? If not, then perhaps you should be. That way you could simply supply the area for the route and it would be able to resolve the url for you. Given your sample URLs you could build your main site with your widget site as an area. Supplying a empty string for the area parameter in the route values would allow you to reference a controller/action in the main site from the widget site.

Resources