Interesting Laravel routing url problems - laravel

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');

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');

Laravel return from controller and continue routing

I haven't found an answer whether it is possible to return from a Laravel controller back to routers and continue searching for another route. Or maybe, another approach would be useful to solve this:
The goal is to have blog articles with url following the domain name like this:
domain.com/url-of-blog-article
so, my route looks like this:
Route::get ('{articleUrl}', 'WEB\BlogController#showArticle');
However, if the article with the requested url doesn't exists, I would like to return back to the router and continue with searching another page. One solution might be to put this route to the end of the router. But, is there a way to return from the controller without a view?
I don't believe that's possible. Moreover, even if it were, I wouldn't recommend it -- your application would waste time searching the database for an article that doesn't exist, and it would go against the router's convention of matching a single route for a given URL.

How to run single controller method from any route in Laravel 4

I really hope my question has been well thought out but here goes.
How do you implement something like
Route::get("/url1", "controller#method");
Route::get("hello/url1", "controller#method");
Route::get("hello/hi/url1", "controller#method");
in Laravel but using something like
Route::get("*/url1", "controller#method");
instead of declaring every route path?
I will explain why this problem has come up. You see the primary url is always changing because its being called from a js file via a location.href call. I could decide to use a primary url variable but its to be deployed via intranet to different servers in organizations and the primary url could change at any time meaning that localhost/project on one system might become localhost:7987/project on another thus breaking the url variable, now thats on one part. On the other hand there are js functions running continuously and when someone navigates to a deeper url, say from localhost/home to localhost/home/event a route call that should be independent of folder breaks
So yeah, I am wondering if theres a way to declare a global route that points to a controller and/or if this is possible in Laravel.
Thanks
Try this:
Route::get('{something}/url1', 'controller#method')->where('something', '*');
Not sure if that will work, but the idea is that you can use where to pass some Regexp to match selected value from route.

codeigniter routing with variable segment

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

Custom profile URL for own site, been though various posts..!

I've been through a few similar posts,
Facebook Like Custom Profile URL PHP
Custom URL / Apache URL Rewriting
But its still not clear, the actual method/process is not available..
Guys , little more guidance would do a lot..
I would like to put forward the questions here:
Users should have a chance to decide what is their url, Just like in case of fb, twitter
for example: www.facebook.com/harry.inaction
I am using the linux, apache, mysql, php environment for this.
Users are identified based on their user id's which get created automatically when they join in
And I fail at the very first step, seriously I don't know get started.
Thanks
It's going to be impossible to put any details as an answer because you've got to build this system of yours and there's more than one way to do it. Design decisions will need to be made based on the way you want things to work and what you already have (they're going to have to work together in some way).
Say you've already got a system for creating users (and it sounds like you do) and you already have a system for viewing profiles. You'll need to extend this system so that you store an extra "my_vanity_url" field in your user table in your database. This field needs to be unique. When a user edits their profile, they have the option of changing this to whatever they want (limiting it to only letters and numbers and dashes for simplicity).
Next, when you display this profile, say it is via /profile.php, your code needs to check a few things.
First it needs to check how it's called, looking at $_SERVER['REQUEST_URI'] you can see either /user/some-vanity-name or /profile.php?u=1234.
If it's the latter, you need to redirect the browser, do a database lookup to see who the user with user_id 1234 is.
Pull the "my_vanity_url" column out of the database for this user and redirect the browser to /user/my_vanity_url_value (replacing my_vanity_url_value with the value of that column).
So now, if you go to http://your.domain.com/profile.php?u=1234, your browser gets redirected and the URL address bar will say http://your.domian.com/user/my_name.
Next, you need to be able to take that unique name and turn it back into the old ugly looking profile page. Two things need to happen here:
You need to extend your profile.php once more to take an optional vanity name as opposed to a user_id
You need to use mod_rewrite to internally route vanity names to /profile.php
For the first thing, you simply look for a different $_GET[] parameter instead of whatever it is for a user_id. Say it's called name: so look at $_GET['name'], see if it exists, if it does lookup the user in the user table whose vanity url name is $_GET['name']. Return the profile of that user.
For the second thing, you just need to put this in the appropriate place in your htaccess file in your document root:
RewriteEngine On
RewriteRule ^/?user/([A-Za-z0-9-]+)/?$ /profile.php?name=$1 [L]
This is just an example for how to implement something like this. It may be completely inapplicable for what you have, but it should give you an idea of what you need to do.

Resources