Laravel - Get route name in filter - php

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

Related

Optional route parameters not working in Lumen 5.7

I had defined my route and controller as following
$router->group(['prefix' => 'api/v1'], function ($router) {
$router->group(
['middleware' => 'auth'], function() use ($router) {
$router->get('/order/get-order-status/{order_id}[/{account_id}]'
, [
'uses' => 'Api\V1\OrderController#getOrderStatus'
, 'as' => 'getOrderStatus'
]
);
});
});
following is the function defination
public function getOrderStatus($orderId, $accountId = false)
{
// my code goes here
}
Here the problem is whenever, I skip the optional account_id from the route, then passed order_id is assigned to the 2nd parameter of the function i,e. accountId. If I pass both params then everything is working as expected. I'm just confused whether something is wrong in my configuration or Lumen itself have some issue with optional route params?
Consider I had triggered http://localhost/lumen/api/v1/order/get-order-status/ORD1234 then ORD1234 is assigned to accountId and '0' is assigned to orderId
optional route parameters are given like below,
$router->get('/order/get-order-status/{order_id}/{account_id?}' // see the ? mark
though I am not sure why 0 is assigned to the orderId,
and usually, the first parameter to the controller method is a request object, so you can easily identify what are the things that the request contains.
public function getOrderStatus(Request $reqeust, $orderId, $accountId = false)
move out your optional parameter route outside the group
$router->group(['prefix' => 'api/v1'], function ($router) {
$router->get('/order/get-order-status/{order_id}[/{account_id}]'
, [
,'middleware' => 'auth',
'uses' => 'Api\V1\OrderController#getOrderStatus'
, 'as' => 'getOrderStatus'
]
);
or like this following code
$router->get('api/v1/order/get-order-status/{order_id}[/{account_id}]',
['uses' => 'Api\V1\OrderController#getOrderStatus',
'middleware' => 'auth',
'as' => 'getOrderStatus'
]
);
I think you should use optional parameter like
{account_id?} rather then [/{account_id}]

Laravel: To pass common parameter value for all route in laravel

** I have Multiple Route in Laravel. I wanna pass common parameter for every Route i,e If i passed POST as a parameter in any route, I need to Call POST controller for all URL.
My Routes are below:
Original URL: www.mydomain.com/home/
Required URL: www.mydomain.com/home/post
Original URL: www.mydomain.com/follow/
Required URL: www.mydomain.com/follow/post
In above URL I have two separate blades like home.blade.php AND post.blade.php,
In Second Example I have two separate blades like follow.blade.php AND post.blade.php. Here Post.blade.php is common.
For Both home and follow must call postcontroller. My Route Controllers . **
//for follow Route
Route::get('Follow', [
'as' => 'Follow',
'uses' => 'PageController#getFollow'
]);
//for Home Route
Route::get('Home', [
'as' => 'Home',
'uses' => 'PageController#getHome'
]);
Post Route controller is below
Route::get('Post', [
'as' => 'Post',
'uses' => 'PostController#getPOST'
]);

How to differentiate parameter vs declared route when routing?

Having the next routes:
Route::get('/apartment/{apartment_name}', 'ApartmentController#getApartmentByName');
Route::get('/apartment/create', [
'uses' => 'ApartmentController#create',
'as' => 'apartment.create'
]);
Route::get('/apartment/edit', [
'uses' => 'ApartmentController#edit',
'as' => 'apartment.edit',
]);
How could I make a difference between the routes
myapp.com/apartment/create and myapp.com/apartment/beach-apartment
I would like to search by the apartment's name with the same URI prefix (apartment/) but with this code I'm always calling the parameter route.
It is because whatever is being called, create or edit, is being matched within the parameter one, /apartment/{apartment_name}, as create or edit equals to the apartment_name.
Just move the parameter one to the lower most line within that block.
Route::get('/apartment/create', [
'uses' => 'ApartmentController#create',
'as' => 'apartment.create'
]);
Route::get('/apartment/edit', [
'uses' => 'ApartmentController#edit',
'as' => 'apartment.edit',
]);
Route::get('/apartment/{apartment_name}', 'ApartmentController#getApartmentByName');
With this configuration, if the /apartment/create or /apartment/edit is not matched, then it will match /apartment/{apartment_name}.

Laravel Add Post Route to Resource Route

I have a Laravel 5.2 app that is using Resource routes. I have one as follows:
Route::resource('submissions', 'SubmissionsController');
I want to add a new Post route to it for a sorting form on my index page.
Route::post('submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
I have placed the Post route above my Resource route in my routes.php.
However, a validation Request named SubmissionRequest that is meant for forms within the Submission Resource is being executed on my new Post route. Here is my SubmissionsController Method.
public function index(SortRequest $req)
{
$submission = new Submission;
$submission = $submission->join('mcd_forms', 'mcd_forms.submission_id', '=', 'submissions.id')->where('user_id', Auth::user()->id);
$data['sort_types'] = [
'name' => 'Name',
'form_type' => 'Type'
];
$data['direction'] = ( !empty($req['asc']) ? 'asc' : 'desc' );
$data['dataVal'] = ( !empty($req['sort_type']) ? $req['sort_type'] : 'submissions.id' );
$submission->whereNull('submissions.deleted_at')->orderBy(
$data['dataVal'],
$data['direction']
);
$data['submissions'] = $submission->get();
return view('submissions.index')->with($data);
}
So, when submitting the sorting form from my index page, it is running the SubmissionRequest validation even though I am specifically calling the SortRequest validation. What am I doing wrong?
I resolved it.
Since my Post route was conflicting with my Get route for submissions.index I added below the Resource route the following:
Route::match(['get', 'post'], 'submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
This allows the route to accept both Get and Post requests by overriding the automatically generated one.
The documentation is here: https://laravel.com/docs/master/routing#basic-routing
Route::match(['get', 'post'], 'submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
in laravel 5 its conflict with #store action

Laravel 5 named route with parameter

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']);

Categories