I'm pretty new on laravel5 and I'm trying to generate dynamically route alias under route.php
This is it:
Route::get('/menu/{category}/{product}/{item}', 'MenuController#listItem')->name('/{category}/{item}');
I already tried with with 'as' and 'uses' and I'm still getting:
/menu/{category}/{product}/{item}
With all parameters replaced by the correct values instead of:
/{category}/{item}
Expounding on what Vinicius Luiz said.
Route::get('/menu/{category}/{product}/{item}', ['as' => 'named.route' , 'uses' => 'MenuController#listItem']);
// to get the actual linke
route('named.route', ['category' => $category->id, 'product' => $product->id, 'item' => $item->id]);
depending, you may not do ->id or anything, you might just pass the whole $category, $product, etc. Depends on how the routing in your controllers is setup.
EDIT:
From your comment, it likes like you want something like:
class MenuController {
public function lisItem($category_name, $product_name) {
$category = Category::where('name', $category_name)->first(['id']);
$product = Product::where('category_id', $category->id)->where('name', $product_name')->first();
}
}
Route::get('/{category}/{item}', ['as' => 'named.route' , 'uses' => 'MenuController#listItem']);
// to get the actual linke
route('named.route', ['category' => $category->id, 'item' => $item->id]);
there is probably a better way to do the queries, but that should work for you.
Try it:
Route::get('/menu/{category}/{product}/{item}', ['as' => 'a.name.to.your.route' , 'uses' => 'MenuController#listItem']);
Related
Help me in a doubt, I have an application in laravel, and I would like to call several Controller in the same Route, is it possible?
I've tried it that way, but it does not work
$api->get('document', ['as' => 'system.api.manager.v1.document.listDoc1', 'shield' => ['system.manager.document.list'], 'any' => true, 'uses' => 'Doc1Controller#grid']);
$api->get('document', ['as' => 'system.api.manager.v1.document.listDoc2', 'shield' => ['system.manager.document.list'], 'any' => true, 'uses' => 'Doc2Controller#grid']);
$api->get('document', ['as' => 'system.api.manager.v1.document.listDoc3', 'shield' => ['system.manager.document.list'], 'any' => true, 'uses' => 'Doc3Controller#grid']);
You can use inheritance.
Define GreatController as parent of
"Doc1Controller, Doc2Controller, Doc3Controller"
then move your standart methods, functions to GreatController. I am doing that way. Also you can define all same methods in _construct()
you can use a closure instead a direct controller call. There you call your controller
i have one issue with the Laravel routes.
I have one function index($sport = '', $date = ''); this function shows me the news for a specific sport and date. But sometimes these parameters are not entered and I want to display all news. This works so fine so good, the problem is with the route.
This is the route code I have used:
Route::get('/news/{sport?}/{date?}.html', ['as' => 'news.index', 'uses' => 'NewsController#index']);
The problem comes when there is no sport and date entered, than the URL is domain.com/news.html, but is not caught with that code. How can i achieve that ?
If you think about news and sport as resources you could have something like that:
// index case news
Route::get('/news.html', ['as' => 'news.index', 'uses' => 'NewsController#index']);
// show case news
Route::get('/news/{date}.html', ['as' => 'news.show', 'uses' => 'NewsController#show']);
// index case sport
Route::get('/news/sport.html', ['as' => 'sport.index', 'uses' => 'SportController#index']);
// show case sport
Route::get('/news/sport/{date}.html', ['as' => 'sport.show', 'uses' => 'SportController#show']);
A different approach would be:
// index case news
Route::get('/news.html', ['as' => 'news.index', 'uses' => 'NewsController#index']);
// sport case
Route::get('/news/{sport}.html' , ['as' => 'sport.index', 'uses' => 'SportController#index'])
->where(['sport' => '[0-9]+']);
// date case
Route::get('/news/{date}.html', ['as' => 'news.show', 'uses' => 'NewsController#show'])
->where(['date' => '[0-9]{4}-[0-9]{1,}-[0-9]{1,}']);
// sport and date case
Route::get('/news/{sport}/{date}.html' , ['as' => 'sport.index', 'uses' => 'SportController#show'])
->where(['sport' => '[0-9]+', 'date' => '[0-9]{4}-[0-9]{1,}-[0-9]{1,}']);
theres so many ways todo that, but for the simplest way, just add another route for displaying all list, Route::get('/news/','NewsController#index');
If you want to keep it simple , you should try this approach
domain.com/news.html?date=2016-11-23&sport=1
if date and sport not found in url you can show all result
and it is easy readable syntax if some one want to bookmark this url.
in my routes file i define a route for my controller method
$routes->connect('/category/:cat', ['controller' => 'Categories', 'action' => 'category']);
My controller method is this
public function category(){
$this->paginate = [
'limit' => 2
];
$this->viewBuilder()->layout('newLayout');
$cat = $this->request->params['cat'];
$id = $this->Categories->findBySlug($cat)->select(['id'])->hydrate(false)->toArray();
$cid = $id[0]['id'];
$category = $this->ProductCategories
->find("all")
->select(['id', 'category_id'])
->where(["category_id" => $cid])
->contain(['Products' => function($q){
return $q->select(['id', 'sku', 'product', 'description', 'slug', 'price', 'off', 'stock', 'product_category_id'])
->where(['status' => 1])
->contain(['ProductImages' => function($q){
return $q->select(['product_id','url']);
}]);
}])->hydrate(false);
$categories = $this->paginate($category);
$this->set(compact('categories'));
$this->set('_serialize', ['categories']);
}
And my url look like this:
http://localhost/mizzoli.com/category/Designer-Saree
Now when i click on cake pagination url change to this
http://localhost/mizzoli.com/categories/category?page=2
But actual url i want is like this
http://localhost/mizzoli.com/category/Designer-Saree?page=2
And also i need to pass some extra parameter with pagination url like color, occasion etc. Please help me to get this. I did not find any solution.
I had a similar issue and resolved it by passing the param I needed inside the third parameter of the route definition. Like this:
$routes->connect('/category/:cat', ['controller' => 'Categories', 'action' => 'category'], ['pass' => ['cat']]);
I have #littleylv from the #cakephp IRC channel on Freenode to thank for this solution.
You can read more about passing parameters to actions via routes here: https://book.cakephp.org/3.0/en/development/routing.html#passing-parameters-to-action
I have this route defined inside a group
Route::group(['domain' => '{subdomain}.test.com'], function () {
Route::get('/models/{id?}', [
'as' => 'car-model',
'uses' => 'CarModelController#details'
]);
});
I want to avoid hardcoding URLs in blade
{{route('car-model', 'ford', '100) }}
but that returs this url
ford.test.com/models
no model id!
Not sure if is relevant but in my controller CarModelController.php
I defined
public function details($subdomain, $id)
why is not sending the id to the generated url? Do I need to send the $subdomain parameter to the detail function?
I found that
{{route('car-model', ['make' => 'ford', 'id' => '100]) }}
works! thanks for watching :)
How can I get current route name in filter? I tried use Route::currentRouteName(); but it's null.
Route::filter('belongsToUser', function(){
dd( Route::currentRouteName() );
exit;
});
Route looks for example:
Route::get('/openTicket/{id}', array('before' => 'auth|belongsToUser', 'uses' => 'MyController#MyAction'));
Your route isn't named, so it's no surprise the route name is null. You need an as parameter.
Route::get('/openTicket/{id}', array(
'as' => 'yourRouteName',
'before' => 'auth|belongsToUser',
'uses' => 'MyController#MyAction'));
http://laravel.com/docs/routing#named-routes