laravel dynamic route groups using year - laravel

I have a site that pulls a lot of data and it is all tied to a specific year. I was trying to think of the best way to add a dynamic route group that included the year so that it would always be showing for the user and that I could use in my models.
Something like www.site.com/2015/dashboard
I can do this using a standard route group, but it requires me to add the route perameters to every method I use.
Is there a better way to use something like this and have the year available to the controller at all times and not have to send it with each route?

Type this in routes.php
$year= Request::segment(1);
Route::group(['prefix'=>$year], function() {
Route::get('dashboard', ['uses' => 'SomeController#someMethod']);
});
And in your controller pick year:
$year = Request::segment(1)

Related

Show Laravel resources API for only specific routes

Is it possible to show new added Laravel resources API fields on specific routes?
Example: I have a UserResource which has 3 fields - id, name and email. I'm returning UserResource in 10 routes. For 11th route added new field - posts.
Is it possible to show new posts field only for 11th route?
Of course you can, so you need to add two things. First inside of your controller in the method where you want to load posts do the following
return UserResourse::collection($users->with('posts'));
Then inside of your UserResourse class add the following
'posts' => PostResourse::collection($this->whenLoaded('posts'))
// or like this if you dont have a PostResouse
'posts' => $this->posts

Route User Role Laravel 5.4

I'm very confused on this situation. I have two routes with on resource name.
Route::resource('product', 'Product\AreaManagerProductController');
Route::resource('product', 'Product\SystemAdminProductController');
I need to make it as one because I have a contextual binding.
$this->app->when(AreaManagerProductController::class)
->needs(ProductInterface::class)
->give(AreaManagerProductRepository::class);
$this->app->when(SystemAdminProductController::class)
->needs(ProductInterface::class)
->give(SystemAdminProductRepository::class);
The contextual binding works fine... but I need to change my route like this.
Route::resource('product_area_manager', 'Product\AreaManagerProductController');
Route::resource('product_system_admin', 'Product\SystemAdminProductController');
I created ProductController and some kind of weird solution.
public function index(){
//Create a conditional statement base on user
return app('App\Http\Controllers\Product\AreaManagerProductController')->index();
}
It may work but it doesn't trigger the middleware... What could be the best practice on this situation. TY
You can have your Route like this
Route::group(['prefix' => 'product', 'namespace' => 'Product', 'middleware' => '<your middleware>'], function() {
Route::resource('area_manager', 'AreaManagerController');
Route::resource('system_admin', 'SystemAdminController');
});
The reason I grouped the route is to reduce redundancy, and the reason i removed Product from the controller name is, as there is a namespace Product already, there is no need of long Class names.
If you wan to access some methods in the AreaManagerController and SystemAdminController just extend the ProductController to these Controllers.
If you want to add some specific middleware for the actions inside these controllers, I have added a middleware clause in the route group which will affect to these controllers, if not needed just remove it.
Hope this helps you.

how to set common controller in group route

I am working on laravel but i have no idea about using route.
i used route group method but i have a question that can we use a common controller in group route
like
I have bunch of routes
Route::group(['prefix' => 'agent'], function(){
Route::get('pay', 'PaymentController#pay');
Route::get('pay/success', 'PaymentController#success');
Route::get('pay/failure', 'PaymentController#failure');
Route::get('credits', 'PaymentController#credits');
Route::get('checkout', 'PaymentController#checkout');
});
As you can see they all are using same route so is there any way to make this as dry as possible i know those are small route but when it goes long line then it become hard to understand i know it's kind of stupid question
is there any attribute like
Route::group(['prefix' => 'agent', 'controller' => 'PaymentController'], function(){
Route::get('pay', 'pay');
Route::get('pay/success', 'success');
Route::get('pay/failure', 'failure');
Route::get('credits', 'credits');
Route::get('checkout', 'checkout');
});
No there isn't any option to define default controller for a route group. But if you have resource routes then it defines all the sub routes by itself, though it's limited to only CRUD routes. You can do something like this if you're interested.
Route::group(['prefix' => 'agent'], function($controller = 'TestController#') {
Route::get('pay', $controller.'pay');
Route::get('pay/success', $controller.'success');
Route::get('pay/failure', $controller.'failure');
Route::get('credits', $controller.'credits');
Route::get('checkout', $controller.'checkout');
});

How do I set dynamic prefixes in Laravel routes

heres the situation - I'm building a site thats region based and I need to set it up so that the first segment of the route is a region i.e. of the type:
www.mysite.com/{region}
Currently I have routes set up like this:
www.mysite.com/businesses
www.mysite.com/businesses/show
I've tried a number of tricks but for some reason I can't get this to work:
www.mysite.com/{region}/businesses
in such a way that I need to filter by the {region} variable and that the {region} variable has to prepend every url in the site, also the {region} variable should also be accessible in the controller. I had a look at the localization options except I don't want to change languages here. I'm looking for implementing something of the following:
www.mysite.com/barcelona/businesses
www.mysite.com/new-york/businesses
I already have a table of all regions and slugs and the required relationships. Just trying to get this to work.
Add the region route on top of all other routes, I have similar feature for a project and that fixed it for me.
Route::get('{region?}/businesses', array(
'as' => 'regionBusinesses',
'uses' => 'RegionBusinessesController#getBusinesses'
))->where('region', '.*');
In your Controller
class RegionBusinessesController extends SomeController {
public function getBusinesses($region) {
return View::make('YOUR_VIEW_NAME')->withBusinesses($this->YOUR_MODEL->FETCH_BUSINESSES_METHOD($region));
}

Parameter in routes

I have the following route
Route::get('compare/{user_id}/{compare_id}', 'CompareController#index');
Since only the owner or members of the working group should have access to this site, I need to filter it. But how can I add the user_id and the compare_id in to the filter?
There's a couple of ways you could do this. Firstly you could just use Request::segment() to fetch each of the required segments of the URI.
A second and better solution is to get the current route with Route::getCurrentRoute().
Route::filter('example', function()
{
$route = Route::getCurrentRoute();
$user_id = $route->getParameter('user_id');
$compare_id = $route->getParameter('compare_id');
});

Resources