I would like to create a dynamic route that allows multiple segments.
example.com/segment1
example.com/segment1/segment2/
example.com/segment1/segment2/segment3
Using this:
Route::get('{slug}', 'PagesController#index');
Gives me my first dynamic segment and of course I could do this:
Route::get('{slug1}/{slug2}', 'PagesController#index');
and so on but I'm sure there's a better way to do this.
I believe one complication when using Route::get('{slug1}/{slug2}', 'PagesController#index'); would be handling all possible inputs so segment1/segment2/ and also foo/bar/. You would probably end up with lots of unnecessary logic.
This may not be the best solution but I believe groups would work well for what you are trying to accomplish.
Route::group(array('prefix' => 'segment1'), function() {
Route::get('/', 'ControllerOne#index');
Route::group(array('prefix' => 'segment2'), function() {
Route::get('/', 'ControllerTwo#index);
Route::get('/segment3', 'ControllerThree#index');
});
});
Maybe a little messy when only dealing with three examples but it could end up being beneficial and provide a nicer hierarchy to work with.
This also has the benefit for using before and after filters. Like for all segment2 endpoints if you want to perform some filter, instead of adding the filter to all the individual endpoints you can just add it to the group!
Route::group(array('before' => 'someFilter', 'prefix' => 'segment2'), function() {
// ... ...
});
Related
I have this code, but it's very verbose. How Can I write that shorter?
Route::get('/transaction/index', 'TransacaoController#index');
Route::get('/transaction/test1', 'TransacaoController#test1');
Route::get('/transaction/test2', 'TransacaoController#test2');
Route::get('/transaction/test3', 'TransacaoController#test3');
Route::get('/transaction/test4', 'TransacaoController#test4');
Not sure if you're talking about the Route Prefixes in Laravel.
Route Prefixes
The prefix method may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with admin:
Route::prefix('admin')->group(function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
});
Extracted from https://laravel.com/docs/5.7/routing
It might depends on how you prefer organize your infrastructure. Writing less not always is synonym of order.
You can use group() to apply middleware or prefixes to your routes:
Route::group(['middleware' => ['custom'], 'prefix' => 'transaction'], function(){
Route::get('/index', 'TransacaoController#index');
Route::get('/test1', 'TransacaoController#test1');
Route::get('/test2', 'TransacaoController#test2');
Route::get('/test3', 'TransacaoController#test3');
Route::get('/test4', 'TransacaoController#test4');
});
Now, there is another option (does not recommended). You can use a unique route making the separation of your logic at controller level:
Route::get('/transaction/{action}', 'TransacaoController#action');
function action($action){
if ($action == 'index'){
// ...
}
}
I am looking to implement a whitelabel solution to my platform and need to implement wildcard subdomains for or system, the only issue is our system sits on a subdomain its self. So I need to filter out anything that comes to a specific subdomain.
// *.website.co.uk
Route::group(['domain' => '{element}.website.co.uk'], function() {
Route::get('/', function ($element) {
dd($element);
});
});
// my.website.co.uk
Route::get('/', 'PagesController#getLogin');
Route::post('/', 'PagesController#postLogin');
However using the code above I get the error:
Undefined variable: element
How would I avoid this error?
A good way is to exclude 'my' using a pattern. Put this code at the top of your routes file:
Route::pattern('element', '(?!^my$)');
Alternatively, that can go in the boot() section of your RouteSericeProvider. To give you a working solution, your code becomes the following (you can tidy up later!)
Route::pattern('element', '(?!^my$)');
Route::group(['domain' => '{element}.website.co.uk'], function() {
Route::get('/', 'PagesController#getLogin');
Route::post('/', 'PagesController#postLogin');
});
An alternative way is to match the 'my' route before matching the {element} route. However, while many do this I think it might be harder to maintain if the ordering of routes isn't clearly explained in the comments.
I'm fairly new to Laravel, so this question may obvious to some.
In the case of running checks per HTTP request, for example User Authentication. Is there a better, more efficient or simple correct way to run these checks. From my initial research it would seem that this could be accomplished using either MiddleWare, eg.
public function __construct()
{
$this->middleware('auth');
}
It also seems like it would be possible using routing groups, eg.
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
Is there any benefits of doing this either of these two ways? Apart from the obvious benefit of not having to put $this->middleware('auth'); in every controller auth would need to be checked.
Thanks
Edit..
After taking on your advice I attempted to utilities the route grouping to control my Auth MiddleWare. But this has seemed to have broken my site.
Route::group(['middleware' => 'auth'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get ( '/redirect/{provider}', 'SocialAuthController#redirect' );
Route::get ( '/callback/{provider}', 'SocialAuthController#callback' );
});
Am I missing something obvious?
You are almost there, just remove the Route::auth():
Route::group(['middleware' => 'auth'], function () {
Route::get('/home', 'HomeController#index');
//add more Routes here
});
The suggested options did not work for me but when I checked the laravel documentation, I found this:
Route::middleware(['web'])->group(function () {
//Your routes here
});
It works for me. Laravel 8.*
There is no real difference, personally i use groups for the standard middleware and put exceptions in the construct
Using Route group is easy for maintenance/modification , other wise you will have to remember each controller where you are using certain middle ware, of course this not a concern in a small medium sized application, but this will be hard in a large application where is lots of controller and references to middle ware.
In Laravel PHP Framework, How can I apply a filter on all routes/pages of the website except one specific route/page?
Update:
It will be great if there is a way other than (route groups)?
use route groups
// unsecured routes.
Route::get('/', 'UserController#getLogin');
Route::group(array('before' => 'yourFilter'), function()
{
// secured by filter `yourFilter`.
Route::controller('route1', 'XxxController');
Route::post('user/save', function() {
// content
});
Route::get('user', 'UserController#getUser');
});
Let assume, the base URL of my application is - http://www.example.com (I have not set anything in any config file to specify this). There are a lot of hard coded urls in the application.
eg. Contact
Now, if I go though the application using an URL like - http://www.example.com/country, is it possible to assign a global base URL, where when I click on contact it will take me to - http://www.example.com/country/contact.
There are a lot of such hard-coded URL, changing it individually will take a lot of time (like appending it with a global variable). Is there any simpler way to do this or is there any config specific for this in laravel? I am fairly new to laravel. Any help would be appreciated.
You can use the somewhat cumbersome solution of applying a filter, as suggested by #worldask, but I think it would be better to set a named route and change all occurrences using a regular expression (any decent editor allows for that). That way, for the lifetime of your application you only need to change the routes in routes.php, and it will be reflected everywhere.
e.g
Route::get('country/contact', ['as' => 'contact',
'uses' => 'SomeController#someFn'];
Contact
Of course, the same principle applies to adding a prefixed group of routes, so you can wrap the entire routes file with a group prefixed by 'country'.
you can try Route Prefixing
Route::group(array('prefix' => 'country'), function(){
Route::get('Contact', 'HomeController#index');
Route::get('another', 'HomeController#index');
});
edit
try route filter
Route::filter('filtername', function($route, $request, $value)
{
if ($route == 'country') {
return Redirect::to(url);
}
});