Change name of route:resource parameter - laravel

I have this route:
Route::group(['middleware' => 'admin'], function () {
Route::resource('backoffice/home', 'Backoffice\Home');
Route::resource('backoffice/ProductBasic', 'Backoffice\ProductBasic');
});
Laravel by default uses "ProductBasic" as parameter. As can be requested with
Request::route()->parameters();
The return on dd() will be
array:1 [▼
"ProductBasic" => "1"
]
I would like to adjust of every var. How can i do that while still using Route::resource as a method of creating my routes.
Edit:
Goals is to use the parameter outside a controller, in a composer. By using 1 parameter for a group of routes (and another for another group...) i can create a dynamic menu.
If(isset(parameter)){
use parameter value to do something
}
I know i can pass parameters to the view and access the values in the view, but i think it's a cleaner solution this way.

Related

Laravel routing with or without parameter in a group

For my application I am trying to create a few routes entries.
One entry to initialise the application and another for AJAX requests.
So my application should hit the initialise function if I type https.test.com/app/drive but also if I want to type some additional parameters at the end something like this: https:test.com/app/drive/specificTabA or https:test.com/app/drive/specificTabB
The problem is that when ever I type https.test.com/drive/specificTabNameA this clashes with the fetchData get route used by my AJAX call.
How can I access the initialise function when hitting this URl https.test.com/app/drive or also hitting something like this: https:test.com/app/drive/specificTabA or https:test.com/app/drive/specificTabB?
Route::group(['prefix' => 'drive'], function () {
Route::get('', 'CustomController#initialise');
Route::get('fetchData', 'CustomController#fetchData');
});
I've done some tests, and came with the following conclusion/solution:
Route::group(['prefix' => 'drive'], function () {
Route::get('fetchData', 'CustomController#fetchData');
Route::get('{param?}', 'CustomController#initialise');
});
CustomerController:
function initialise($param = null)
{
...
}
Note that by changing the order of the routes you will actually load the correct route.
When you visit /drive/fetchData it will load fetchData route
When you visit /drive/ it will load initialise route without arguments
When you visit /drive/xyz it will load initialise route with $param being xyz
Hope it helps :)
My friend I want to get your attention to Laravel docs https://laravel.com/docs/5.0/routing#route-parameters especially this one Route Parameters. You can tell router that this route can have parameter but also can not have it. Look at this example
Route::get('/{specific?}')
Now you can get this specific parameter in your Controller function after request
public function initialize (Request $request, $specific = null)
Set it default to null as this param can both be past and not, so it should have some default value.
Good luck ;)
The following should work for you:
Route::group(['prefix' => 'drive'], function () {
Route::get('fetchData', 'CustomController#fetchData');
Route::get('{path?}', 'CustomController#initialise')->where(['path' => '.*']);
});
This will allow the following path:
/drive => initialise
/drive/1 => initalize
/drive/1/2/3 => initalize
/drive/fetchData => fetchData
Adding ->where(['path' => '.*']) will route any path to initalize, e.g. /1, /1/2, /1/2/3.
If you only want to allow the path to be one level deep you can remove the where:
Route::get('{path?}', 'CustomController#initialise');

Change default URL param name when using Route::resource

When using Route::resource(), Laravel of course 'chooses' somethings for you, such as route names and methods.
I know how to override, for example, the allowed routes/methods:
Route::resource('user', 'UserController', array('only' => array('index', 'show')));
But I now need to override the URL param name that Laravel sets for the user routes. By default, in the example above, it will be user. But, I want it to be user_id.
Does Laravel provide a way to set this, when using Route::resource?
So that, for example, I would end up with the route:
mydomain.com/users/{user_id}
rather than:
mydomain.com/users/{user}
Thanks
Found it. Yes, Laravel does provide a way to override this when using Route::resource().
Route::resource('users', 'UserController')->parameters([
'users' => 'user_id'
]);
The key of the element in the array in the argument to 'parameters' is the same as what you enter as the first argument of the 'resource' method (not the 'singular' version or something)

Multi-language site. Set routing's rules

I was created this simple routing
Route::group(['prefix' => '{lang}'], function(){
Route::get('/hello', function($lang){
App::setlocale($lang);
return view('welcome');
});
});
It works of course, but in this example I returned only view. I prefer returned controller's method so in my previous project I realize routing in this way:
Route:get('/hello', [
'uses' => 'MyController#myMethod',
'as' => 'myMethod'
]);
How can I use localization and returned controller's method.
You can implement it through the Accept-Language header or the parameter that will determine which language, for example /route/{language}. Next, send a request to the rout, which will call the method in the controller and already in the method to check - which language to use.
Another option is to create multiple routines for multiple languages. And ask for rows depending on the language. There is a minus, if many languages ​​are used, you need to create a lot of routes

Going to next matching route if no model was found

Let's assume we have 2 such routes:
Route::get('{categoryitem}', ['as' => 'category_index', 'uses' => 'CategoryDisplayController#index']);
Route::get('{entryitem}', ['as' => 'entry', 'uses' => 'EntryController#show']);
There are no parameter constraints for parameters and 2 following route model bindings are defined:
Route::bind('categoryitem', function($slug)
{
return Category::whereSlug($slug)->root()->firstOrFail();
});
Route::bind('entryitem', function($slug)
{
return Entry::whereSlug($slug)->firstOrFail();
});
Now let's assume that URL we run is http://project/something. Is it possible to make Laravel look first in route categoryitem for something slug and in case no model is found it will look in the second route for entry with slug something? I haven't found solution for this other than adding some prefix/suffix to route
"Skipping" routes on a condition is not possible at the moment. Although there is a request on github that would allow skipping on a condition if implemented by the framework...
In the meantime you will have to write one route that catches them all. And determine inside of that route (or controller if you wish) if the item exists.
Route::get('{slug}', function($slug){
// check if categoryitem exists
// else check if entryitem exists, etc...
});
Obviously if this becomes a bit two much code move it into a controller

Is there a way to set a default url parameter for the Route class in Laravel 4?

I'm trying to set up sub-domain based routing in Laravel 4 and I've hit a bit of an annoyance...
My route group looks like this:
Route::group(array('domain' => '{company}.domain.com'), function() {
// ...
});
Which seems to work fine, however, I need to specify the company parameter for every route/url I generate. I.e:
{{ HTML::linkRoute('logout', 'Logout', ['company' => Input::get('company')]) }}
Is there any way to specify the company parameter as static/global, so it is automatically added to any links I specify, unless otherwise overwritten/removed?
Unfortunately, no (I haven't seen any evidence in the router or HTMLBuilder that you can). You could, however, make an HTML macro... Example:
HTML::macro('lr', function($link, $title) {
$company = !empty(Input::get('company')) ? Input::get('company') : "";
return HTML::linkRoute($link, $title, ['company' => $company]);
});
Then call it - instead of HTML::linkRoute, use HTML::lr('logout', 'Logout')
Just an idea.

Resources