Side By Side Static Routes & Wild Card Routes In Laravel - laravel

I have a use case for a Laravel website I am working on to have some static routes sitting at the exact same level as the main wild card route.
EG:
Route: /store/cart Static Route
Route: /store/checkout Static Route
Route: /store/* Dynamic Route
Route: /store// Dynamic Route
Route: /* Dynamic Route
Been trying to figure out how to implement this routing structure in Laravel and while the static routing rules work fine as soon as I add the wild card routes I wind up with the wild card route trying to catch the static routes as well.
How would I be able to add routing rules to support this?

change the conflicting routes to non conflicting.
Route: /store/cart Static Route => this is ok
Route: /store/checkout Static Route => this is ok
Route: /store/* Dynamic Route => /store/id/{id}
Route: /store// Dynamic Route => /store
Route: /* Dynamic Route => remove this and be specific by having more routes for the needs

Us global constraint
Route::pattern('all','.*');
Then define your routes in order
Route::('store/cart', function () {});
Route::('store/checkout', function () {});
Route::('store', function () {});
Route::('store/{all}', function ($all) {});
Route::('{all}', function ($all) {});

Managed to get it working.
The first step was to use global constraint route patterns per #Aboalnaga...
Route::pattern('variableName','.*');
Each variable was defined with this route pattern to make it a wild card pattern.
The next step was to ensure route ordering. It appears as though when handling routing Laravel will work its way through the route list in order. As soon as it finds the first matching route it will stop there and run that route. So in order to handle a route chain in the form of domain.com/store/cat-1/productwhere the request could be for domain.com, domain.com/store, domain.com/store/cat-1, domain.com/store/cat-1/product, or domain.com/some-content-page-from-database the route needed to be defined as...
Route::get('/store/shopping-cart', 'onlineStore#showCart');
Route::get('/store/checkout', 'onlineStore#showCheckout');
Route::get('/store/checkout/payment', 'onlineStore#showPayment');
Route::get('/store/checkout/success', 'onlineStore#showPaymentSuccess');
Route::get('/store/checkout/error', 'onlineStore#showPaymentError');
Route::get('/store/{category}', 'onlineStore#showCategory');
Route::get('/store/{category}/{product}', 'onlineStore#showProductDetails');
Route::get('{article}', 'articles#showArticle');
By defining the routes in order and having the variable route defined as the last route at that level the variable route will only be triggered if the preceding routes don't match.

Related

Laravel if route condition not met then ignore

I have the following 2 routes:
Route::get('/{category}/{slug?}', [CategoryController::class, 'index'])->name('category.index')->where('slug', 'trending|subtitled|feature');
This route
Route::get('/cookies-policy', [PageController::class, 'cookiesPolicy'])->name('cookiesPolicy');
When i try to access the second route, the first route controller is accessed instead.
How do i add a condition to the first route, that if the first segment isn't trending,subtitled or feature then ignore? I thought i accomplished this with: ->where('slug', 'trending|subtitled|feature')
You would need to adjust your where to be assigning the pattern to the category route parameter not the slug parameter:
->where('category', ...);

laravel: Route [dashboard] not defined

when admin login then he redirects to dashboard page but by clicking on its main button(like home button) it shows error, i added this code: <a href="{{route('dashboard')}}"> in it but it says:
Route [dashboard] not defined
this is my route:
Route::get('/admin/dashboard','AdminController#dashboard');
any solution to resolve this issue
Give the name to Route.
Route::get('/admin/dashboard','AdminController#dashboard')->name('dashboard);
or
Route::get('/admin/dashboard',[
'uses' => 'AdminController#dashboard',
'as' => 'dashboard']);
As shown in the docs:
route()
The route function generates a URL for the given named route:
$url = route('routeName');
as we see, it generates a URL for the given named route, so you have to provide a name for your route like the following from docs too:
Named Routes
Named routes allow the convenient generation of URLs or
redirects for specific routes. You may specify a name for a route by
chaining the name method onto the route definition:
Route::get('user/profile', function () {
// })->name('profile');
You may also specify route names for controller actions:
Route::get('user/profile', 'UserProfileController#show')->name('profile');
So add a name to your route:
Route::get('/admin/dashboard','AdminController#dashboard')->name('dashboard');
Hope it helps

Laravel 5: Handling Dynamic and Static routes

Is there a way in Laravel5 where I could define routes that handles dynamic routes without conflicting with current static routes? Something similar below:
// Dynamic routes
Route::get('{permalink}', function($permalink) {
//look for matching username on the table (bind perhaps?)
});
// Static routes
Route::get('home', 'HomeController#index');
Route::get('products', 'ProductController#index');
Any ideas, guys? Thanks.
Static and dynamic routes shouldn't conflict with each other. Just put static routes higher than dynamic ones.

How to config Laravel Router some route rule work on different path?

I deployed my Laravel 5 project on A.com, I also want put it under B.com/a. For some reason, the /a path I should handle it in router.
So in router I write:
Route::get('post','PostController#index');
Route::get('a/post','PostController#index');
It's not a good way because there is redundancy, especially there are a lot of other route rules.
In the doc, there is only {xx}? to handle optional param, but in my project, it's not param instead of a static string.
It's there any better way to combine two lines?
I'd use a route prefix within a foreach loop. That'd allow you to quickly and easily manage the prefixes on your routes while keeping them all in one place.
foreach([null, 'a'] as $prefix) {
Route::group(['prefix' => $prefix], function () {
// Your routes here
});
}
The routes not prefixed will take precedence as their routes would be generated first in this case. You could just as easily swap the array around if necessary.
If you really wanted to do it in a single route definition you could do it using a regular expression to match the route.
Route::get('{route}', function () {
dd('Browsing ' . app('request')->path());
})->where('route', '(a/)?post');
But it's clearly not very clean/readable so probably not recommended.
You can do this:
RealRoutes.php:
Route::get('post','PostController#index');
// ... include all of your other routes here
routes.php:
include('RealRoutes.php');
Route::group(['prefix' => 'a/'], function () {
include('RealRoutes.php');
});
There's probably a better way to solve this using a lambda function or similar but the above should work as a quick solution.

Laravel routing & filters

I want to build fancy url in my site with these url patterns:
http://domain.com/specialization/eye
http://domain.com/clinic-dr-house
http://domain.com/faq
The first url has a simple route pattern:
Route::get('/specialization/{slug}', 'FrontController#specialization');
The second and the third url refers to two different controller actions:
SiteController#clinic
SiteController#page
I try with this filter:
Route::filter('/{slug}',function()
{
if(Clinic::where('slug',$slug)->count() == 1)
Route::get('/{slug}','FrontController#clinic');
if(Page::where('slug',$slug)->count() == 1)
Route::get('/{slug}','FrontController#page');
});
And I have an Exception... there is a less painful method?
To declare a filter you should use a filter a static name, for example:
Route::filter('filtername',function()
{
// ...
});
Then you may use this filter in your routes like this way:
Route::get('/specialization/{slug}', array('before' => 'filtername', 'uses' => 'FrontController#specialization'));
So, whenever you use http://domain.com/specialization/eye the filter attached to this route will be executed before the route is dispatched. Read more on documentation.
Update: For second and third routes you may check the route parameter in thew filter and do different things depending on the parameter. Also, you may use one method for both urls, technically both urls are identical to one route so use one route and depending on the param, do different things, for example you have following urls:
http://domain.com/clinic-dr-house
http://domain.com/faq
Use a single route for both url, for example, use:
Route::get('/{param}', 'FrontController#common');
Create common method in your FrontController like this:
public function common($param)
{
// Check the param, if param is clinic-dr-house
// the do something or do something else for faq
// or you may use redirect as well
}

Resources