MVC 2 Routing - {Controller}.com/{action}/{id} - Is it Possible? - model-view-controller

We have several different domains hosted on an in-house server. They all represent different brands owned by our company and we would like integration between each domain, sharing models, views, resources etc.
What I'd like to do is have {Controller} as the actual domain so it would look like http://{Controller}.com/{Action}/{Id}.
Is this possible? I've seen people do it with sub-domains.
AND, is it worthwhile or is there an easier way to accomplish the same.
If so, does anyone have any suggestions on how I can test this on Localhost?

Yes, it's possible and not all that different to routing based on sub-domains. If you look at this example, he looks at the Host header, splits on the '.' and then takes the first element in the array. You'll simply be taking the second-last element in that array (since "com" is the last element). Basically, in your GetRouteData override, you do something like this:
// Retrieve the url - and split by dots:
var url = httpContext.Request.Headers["HOST"];
var urlParts = url.Split(".");
var routeData = new RouteData(this, new MvcRouteHandler());
routeData.Values.Add("controller", urlParts[urlParts.Count - 2]);
(error-checking and validation not included here, obviously)
As for testing on localhost, you can simply add the domain names you want to test to your hosts file, pointing at 127.0.0.1.

Related

How to instruct AEM to look for a specific resource bundle translations

I have two websites and code base is same.
SiteA
SiteB
apps/company/components
How to pick different resource bundle translations for SiteA, SiteB in i18n ?
Thanks,
Sri
Can you please be specific, is this for server side translations or client side translations? For client-side translations, I had an issue with OTB ResourceBundleExportServlet detailed here.
sling:basename way:
Add a property sling:basename to mix:language node. Say sling:basename="siteA"
Pass the basename during bundle lookup. request.getResourceBundle("siteA", locale);
This will return keys from the specific basename only.
Client-side custom bundle exporter:
Keep separate dictionaries for SiteA and SiteB. For example: /apps/company/sitea/i18n, /apps/company/siteb/i18n.
If splitting dictionary is not possible, keep a nomenclature in your labels to identify site. For example all labels should be prefixed with siteA/siteA. Like siteA.clickhere, siteB.clickhere
Create custom servlet similar to ResourceBundleExportServlet. Keep the path as /libs/company/i18n/dict.
The custom servlet will check siteA or siteB from slingrequest and return respective labels only. Filtering the labels based of dictionary path(step 1) or prefix(step 2)
Create overlay to /libs/clientlibs/granite/utils/source/I18n.js. Change the urlPrefix to
var urlPrefix = "/libs/company/i18n/dict.";
Now the client side i18n lookup will pull entries from the custom exporter rather than OTB exporter
Server-side Resolver:
To differentiate sitea or siteb labels we need step 1 or 2 from above.
Once we know to identify site specific labels, we need only a helper util that checks site from request and resolves from specific dictionary or prefix
Hope this helps.

One route for multiple controllers

I'am new to Laravel 5 and have one small problem with routing.
I would like to be able to set one route for multiple controllers.
Lets say I have controlers: one, two, three.
I want to create something like (that is not working of course):
Route::get('{controller}', $controller.'#index')->where('controller', '(one| two|three)+');
So when I will type:
examlpe.com/two
It will call controller two and index method, and it will work in same way for one and three controllers, but not for others (this is why I used 'where' here).
What I am trying to do:
I have to create an app divided to 20 or even more profiles.
Each profile will be used by different users working on different datasets (dataset for group 'one' will be never available for group 'two', etc.).
Each profile will use similiar main functions (about 4-5), but there will be many differences between them (mostly some rules that will have an influence to the function results and dataset presentation).
I can 'extract' core of those functions to the single controller that will be extended by controlers dedicated to each group so then I will be able to adjust functions results to each profile (according to those users needs).
My idea was to have it all separated per profile so then I will have profiles like in example: one, two, three, ...
Then each profile will have slighlty different url so it will be easily recognized, and will contain also dataset id, for ex. (process, view, closed are methods from controller, available for all group, but have some differences as I wrote before):
examlpe.com/two/process/12
examlpe.com/two/view/34
examlpe.com/one/process/66
examlpe.com/three/closed/54
Hope that is clear...

How to build CodeIgniter URL with hierarchy?

I know this doesn't exactly match the form of www.example.com/class/function/ID/, but what I want to display to the user would make more sense.
This is what I would like to do:
www.example.com/project/id/item/item_id/
So, an example would be:
www.example.com/project/5/item/198237/
And this would be the behavior:
www.example.com/project/ --> This would show a list of projects (current implementation)
www.example.com/project/5/ --> This would show a list of items on project 5 (current implementation)
www.example.com/project/5/item/ --> This wouldn't really mean anything different than the line above. (Is that bad?)
www.example.com/project/5/item/198237/ --> This would show details for item 198237.
So, each item is directly associated with one and only one project.
The only way I can think how to do this is to bloat the "project" controller and parse the various parameters and control ALL views from that "project" controller. I'd prefer not to do this, because the model and view for an "item" are truly separate from the model and view of a "project."
The only other solution (that I am currently implementing and don't prefer) is to have the following:
www.example.com/project/5/
www.example.com/item/198237/
Is there any way to build a hierarchical URL as I showed at the beginning without bloating the "project" controller?
There are 3 options, sorted by how practical they can be:
Use URI Routing. Define a regular expression that will use a specific controller/method combination for each URL.
Something like that could help you, in routes.php:
$route['project/'] = 'project/viewall';
$route['project/(.+)'] = 'project/view/$1';
$route['project/(.+)/item/'] = 'project/view/$1';
$route['project/(.+)/item/(.+)'] = 'item/view/$2';
That is, considering your controllers are item and project respectively. Also note that $n in the value corresponds to the part matched in the n-th parenthesis.
Use the same controller with (or without) redirection. I guess you already considered this.
Use redirection at a lower level, such as ModRewrite on Apache servers. You could come up with a rule similar to the one in routes.php. If you are already using such a method, it wouldn't be a bad idea to use that, but only if you are already using such a thing, and preferably, in the case of Apache, in the server configuration rather than an .htaccess file.
You can control all of these options using routes.php (found in the config folder). You can alternatively catch any of your URI segments using the URI class, as in $this->uri->segment(2). That is if you have the URL helper loaded. That you can load by default in the autoload.php file (also in the config folder).

CakePHP, organize site structure around groups

So, I'm not quite sure how I should structure this in CakePHP to work correctly in the proper MVC form.
Let's, for argument sake, say I have the following data structure which are related in various ways:
Team
Task
Equipment
This is generally how sites are and is quite easy to structure and make in Cake. For example, I would have the a model, controller and view for each item set.
My problem (and I'm sure countless others have had it and already solved it) is that I have a level above the item sets. So, for example:
Department
Team
Task
Equipment
Department
Team
Task
Equipment
Department
Team
Task
Equipment
In my site, I need the ability for someone to view the site at an individual group level as well as move to view it all together (ie, ignore the groups).
So, I have models, views and controls for Depart, Team, Task and Equipment.
How do I structure my site so that from the Department view, someone can select a Department then move around the site to the different views for Team/Task/Equipment showing only those that belong to that particular Department.
In this same format, is there a way to also move around ignoring the department associations?
Hopefully the following example URLs clarifies anything that was unclear:
// View items while disregarding which group-set record they belong to
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id
http://www.example.com/Departments
// View items as if only those associated with the selected group-set record exist
http://www.example.com/Department/HR/Team/action/id
http://www.example.com/Department/HR/Task/action/id
http://www.example.com/Department/HR/Equipment/action/id
Can I get the controllers to function in this manner? Is there someone to read so I can figure this out?
Thanks to those that read all this :)
I think I know what you're trying to do. Correct me if I'm wrong:
I built a project manager for myself in which I wanted the URLs to be more logical, so instead of using something like
http://domain.com/project/milestones/add/MyProjectName I could use
http://domain.com/project/MyProjectName/milestones/add
I added a custom route to the end (!important) of my routes so that it catches anything that's not already a route and treats it as a "variable route".
Router::connect('/project/:project/:controller/:action/*', array(), array('project' => '[a-zA-Z0-9\-]+'));
Whatever route you put means that you can't already (or ever) have a controller by that name, for that reason I consider it a good practice to use a singular word instead of a plural. (I have a Projects Controller, so I use "project" to avoid conflicting with it.)
Now, to access the :project parameter anywhere in my app, I use this function in my AppController:
function __currentProject(){
// Finding the current Project's Info
if(isset($this->params['project'])){
App::import('Model', 'Project');
$projectNames = new Project;
$projectNames->contain();
$projectInfo = $projectNames->find('first', array('conditions' => array('Project.slug' => $this->params['project'])));
$project_id = $projectInfo['Project']['id'];
$this->set('project_name_for_layout', $projectInfo['Project']['name']);
return $project_id;
}
}
And I utilize it in my other controllers:
function overview(){
$this->layout = 'project';
// Getting currentProject id from App Controller
$project_id = parent::__currentProject();
// Finding out what time it is and performing queries based on time.
$nowStamp = time();
$nowDate = date('Y-m-d H:i:s' , $nowStamp);
$twoWeeksFromNow = $nowDate + 1209600;
$lateMilestones = $this->Project->Milestone->find('all', array('conditions'=>array('Milestone.project_id' => $project_id, 'Milestone.complete'=> 0, 'Milestone.duedate <'=> $nowDate)));
$this->set(compact('lateMilestones'));
$currentProject = $this->Project->find('all', array('conditions'=>array('Project.slug' => $this->params['project'])));
$this->set(compact('currentProject'));
}
For your project you can try using a route like this at the end of your routes.php file:
Router::connect('/:groupname/:controller/:action/*', array(), array('groupname' => '[a-zA-Z0-9\-]+'));
// Notice I removed "/project" from the beginning. If you put the :groupname first, as I've done in the last example, then you only have one option for these custom url routes.
Then modify the other code to your needs.
If this is a public site, you may want to consider using named variables. This will allow you to define the group on the URL still, but without additional functionality requirements.
http://example.com/team/group:hr
http://example.com/team/action/group:hr/other:var
It may require custom routes too... but it should do the job.
http://book.cakephp.org/view/541/Named-parameters
http://book.cakephp.org/view/542/Defining-Routes
SESSIONS
Since web is stateless, you will need to use sessions (or cookies). The question you will need to ask yourself is how to reflect the selection (or not) of a specific department. It could be as simple as putting a drop down selection in the upper right that reflects ALL, HR, Sales, etc. When the drop down changes, it will set (or clear) the Group session variable.
As for the functionality in the controllers, you just check for the Session. If it is there, you limit the data by the select group. So you would use the same URLs, but the controller or model would manage how the data gets displayed.
// for all functionality use:
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id
You don't change the URL to accommodate for the functionality. That would be like using a different URL for every USER wanting to see their ADDRESS, PHONE NUMBER, or BILLING INFO. Where USER would be the group and ADDRESS, PHONE NUMBER< and BILLING INFO would be the item sets.
WITHOUT SESSIONS
The other option would be to put the Group filter on each page. So for example on Team/index view you would have a group drop down to filter the data. It would accomplish the same thing without having to set and clear session variables.
The conclusion is and the key thing to remember is that the functionality does not change nor does the URLs. The only thing that changes is that you will be working with filtered data sets.
Does that make sense?

Optional URL parameters with url routing in webforms

Playing with the new(ish) url rewriting functionality for web forms, but I'm running into trouble trying to declare parameters as optional.
Here's the scenario. I've got a search function which accepts two parameters, sku and name. Ideally I'd like the URL for this search function to be /products/search/skuSearchString/nameSearchString. I also have various management pages that need to map to things like /products/management/ or /products/summary/. In other words, the last two parameters in the URL need to be optional - there might be one search string, or two, or none.
This is how I've declared my virtual URL:
Friend Const _VIRTUALURL As String = "products/{action}/{sku}/{*product}"
And added the following defaults:
Me.Defaults = New Web.Routing.RouteValueDictionary(New With {.sku = "/"})
Me.Defaults = New Web.Routing.RouteValueDictionary(New With {.product = "/"})
I have two problems with this setup. The most pressing is that the url seems to expect an sku parameter. So, /products/summary/ cannot be found but /products/summary/anyTextAtAll/ maps to the correct page. You get the same result whether the defaults are set to "/" or "". How do I ensure both sku and product parameters are optional?
The second is more a matter of interest. Ideally, I'd like the url to be able to tell whether or not it's got a product search string or a url search string. The obvious way to do this is to make one or the other default to a value I can just pick up and ignore, but is there a neater way of handling it?
I'm not sure I entirely understood the question, but I have some comments about what you've shown so far:
The manner in which you're setting defaults seems incorrect. You're first setting a default value dictionary with a value for "sku". You're then replacing the default value dictionary with a value for "product".
A default value of "/" is unlikely to be what you want. In this case it sounds like you want a default value of just "" (empty string).
Try something like:
Me.Defaults = New Web.Routing.RouteValueDictionary(New With {
.sku = "",
.product = "" })
My VB skills are rather weak, so the syntax I showed might not be exactly right.
I think that if you change both of these then you should be good to go.

Resources