codeigniter routing with variable segment - codeigniter

I've been reading the codeigniter manual about routing but I don't understand how to apply the feature to my situation.
I'm trying to design the following URI
example.com/shopName/product/2/description
example.com/shopName/product/2/gallery
Where shopName is the name of the shop (ie. different every time).=
I'm in the beginning of the project so my controllers are open to alterations.
I've been thinking of doing something like the structure below, as a workaround, but it is definitely not what would be ideal.
example.com/shop/pdesc/2
example.com/shop/pgallery/2

First change route like this:
$route['shopName/(:any)/(:num)/(:any)'] = 'shopname/$1/$2/$3';
You may change this as per your design.
Then create controllers. Read this guide for details. link

Related

How to differentiate between two dynamic url in Laravel

I have two dynamic url with simillar structure. For example, lets say, Product page and category page.
I have set both pages in
Route::get('/{product}', [UsersController:: class, 'productDetail']);
Route::get('/{category}', [UsersController:: class, 'categoryProducts']);
But when I click on url which suppose to go in category page, it redirect to product page only because of same structure. How I can differentiate both URLs for Laravel without altering their url structure?
I don't think this can be done without modifying the URL pattern at least a little bit.
If you do something like /50?type=category then in the show method you can use the query parameter to determine which table to look at. But you'll have to use the same show method and I don't recommend doing it this way.
I hope someone else will be able to shine some more light on the matter.
this is the best practice for your case to make yourapi Resful
Route::get('/product/{product-id}', [UsersController:: class, 'productDetail']);
Route::get('/product/categories, [UsersController:: class, 'categoryProducts']);
learn more about Restful api here https://restfulapi.net/resource-naming/
This should be done by calling index, update diff() function. You can try by using the below:
Route::get('/category/{slug}', 'site\categorycontroller#show')->name('category.show');
Route::get('/product/{slug}', 'site\productcontroller#show')->name('product.show');

how to implement Multi-Tenant functionality in asp.net-core

I have an Asp.net Core application I want to be able to allow multiple/ different Tenant(Client)to access the same application but using different url's. I have common database for all tenant(client).
So It is the main part I want to host my application in a domain say... www.myapplication.com then allow different Tenant(client) to access the same application using
1.www.TenantOne.myapplication.com
2.www.TenanatTwo.myapplication.com.
3.www.{TENANCY_NAME}.myapplication.com
I can't find any info on how to do this and I'm stuck.
How to do it? Please provide the code. Thanks.
As Saravanan suggested these types of questions don't belong here on SO. To get you started, I suggest you start looking if there are any frameworks such as SaaSKit available to add a multi tenancy layer to the pipeline.
The essential part is to know where each request comes from. Using subdomains is a good way to achieve that and middleware is a good place to 'identify' your tenant. You could have a database to persist the tenants but the implementation is entirely up to you. I also wrote a little article on the subject. Although it isn't ASP.NET Core, the principles still apply.
The approach I believe you are looking for is similar to the article at the url below.
https://dotnetthoughts.net/building-multi-tenant-web-apps-with-aspnet-core/
In it, the author splits the requesting URL into an array of strings delimited by the dot in the address. The variable 'subdomain' is then set to the first element of that array. In your question, it looks like you may want to use the second element in the array, but you get the idea.
var fullAddress = actionExecutingContext.HttpContext?.Request?
.Headers?["Host"].ToString()?.Split('.');
var subdomain = fullAddress[0];
//do something, get something, return something
How you use this data is up to you. The author of the article created a filter attribute, but there are many possibilities such as passing the tenant name as a parameter to a service function.
Sorry,you have to get something to start with and then come back for the people to help you with.
I would say that this is all of a domain based wild card mapping and change in your authentication logic to get the tenant id from the URL. Once you identified the tenant, you just login and then take it forward. Like you might be having a database with the tenant details like
tenant1 | tenant1.company.com | guid-ofthe-tenant | etc...
Once you get the URL, you lookup in the above table and get the tenant code and then you choose the login mode and then proceed.
In case you have tried something yet, we would be happy to point you if it does not work yet.

Interesting Laravel routing url problems

I've got (what is to me) an interesting url question.
This is my situation. I have what will be a user populated database, so I cannot be sure how many subareas I will have.
I will always have an area, one or more subareas, and a location that ends my url.
example: /area/subarea1/subarea2/location
This is slightly simplified from what I need. I need to be able to service the following urls as well;
/area/subarea1/location
/area/subarea1/subarea2/subarea3/location)
My routes look something like this:
Route::get('area/{subarea1}', 'SubareaController#show');
Route::get('area/{subarea1}/{location}', 'LocationController#show');
Route::get('area/{subarea1}/{subarea2}', 'SubareaController#show2');
Route::get('area/{subarea1}/{subarea2}/{location}', 'LocationController#show2');
So the problem here is that my routes are overriding each other, because they are essentially the same.
My question is this. Is there any way to differentiate these routes when they have the same url structure? And if not, is there a better way to handle multiple subareas between an area, and a location?
EDIT
Ok I've been tried naming my routes, but I can't seem to be able to use the named routes correctly with all my parameters in the view. I may look into the area/{subarea1}/subarea1/{subarea2}/subarea2 solution, even though I would rather not have the longer URL.
This happens because Laravel has no way to distinguish each route from the other. For example, it would route these 2 url's to the same action:
example.com/area/my-subarea-1/my-location
example.com/area/my-subarea-1/my-subarea-2
So you need different paths. Try this:
Route::get('area/subarea1/{subarea1}', 'SubareaController#show');
Route::get('area/subarea1/{subarea1}/location/{location}', 'LocationController#show');
Route::get('area/subarea1/{subarea1}/subarea2/{subarea2}', 'SubareaController#show2');
Route::get('area/subarea1/{subarea1}/subarea2/{subarea2}/location/{location}', 'LocationController#show2');

Aliasing ID parameter in URL as string in ASP.NET MVC 4

What i'm trying to do is to rewrite URLs to make them more SEO friendly but i still want to pass a parameter as an int ID.
For example, a URL pointing to a news article might look like this:
"www.domain.com/category-id/article-id" or "domain.com/5/3"
What i want to do is to rewrite the URL everywhere so that the title of the category and the title of the article are written into the URL so it becomes f.x. "domain.com/politics/some-title" but i still want to pass the ID of the article as an argument to the controller action. This is less important for the category but it's something i want to do with the article-id since it's unique but the title might not be.
I have checked out Attribute Routing and looked through some Routing guides and questions but haven't found anything that lets me implement this functionality. I've just started using ASP.NET MVC so i haven't been able to look into anything too advanced.
Thanks in advance.
I would advice to make the article title unique and from the controller action you have to get the article based on the title.
I see you are trying to group the articles based on category. When I initially created my blog I thought the same-thing but soon realized it's not a flexible approach because of couple of reasons.
Say you wrote one article with name some-title and dropped it under a category say politics and so the url will be domain.com/politics/some-title but at a later point of time you thought to move the article to another category say 'international-politics' therefore your url now has to be changed to domain.com/international-politics/some-title and you break the old url and whoever has bookmarked that link will now receive 404. A better way would be organize the urls based on the posted date and that's not going to change something like http://domain.com/archive/yyyy/mm/dd/unique_title
Sometimes you want to label an article with more than one category and at that time a tag based approach will become a better choice compared to category based approach.
Quick and dirty solutions:
1) domain.com/categoryName/articleID/articleName/
2) domain.com/date/categoryName/articleName (date should help make articleName unique)
3) domain.com/categoryName/articleName?id=xxx
Nothing fancy, but those approaches will work.

codeigniter single application multiple databases

I will be shifting my code base to the new 2.0 framework and just had a few questions on the following points
My urls will be as follows
example.in/city1
example.in/city1/admin
example.in/city2
example.in/city2/admin
I would like to know what would be the best way to switch the database based on the city in the url? .
In my webroot i will have directories like city1, city2 each containing an index.php file which points to the single code base.
Now i only need a way to switch the database based on the url entered.
Regards,
Sheldon
I completely agree with #poelinca and #Ross, you should definitely consider rethinking your design.
however for reference issues this topic was already covered in the CodeIgniter forums pretty well over here
Basically you just configure another set of $db items per connection and then request the connection based on those configuration parameters by the CodeIgniter framework, by providing the new $db as a parameter for the DB class constructor.

Resources