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

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

Related

Admin route group with prefix, middleware and named routes

I have some named routes in a controller named VehicleController:
vehicle.index
vehicle.show
And then I have an admin section, where I have defined a route group with prefix and middleware. In this section I have a resource controller name AdminVehicleController to handle CRUD tasks for the Vehicle (not sure if this is best practice) with the following routes:
vehicle.index
vehicle.create
vehicle.store
...
However these named routes are conflicting. My routes web.php looks like this for now:
Route::get('vehicles', 'VehicleController#index')->name('vehicle.index');
Route::get('vehicle/{vehicle}', 'VehicleController#show')->name('vehicle.show');
Route::group(['prefix' => 'admin', 'middleware' => 'is.admin'], function () {
Route::get('/', 'AdminDashboardController#index');
Route::resource('vehicle', 'AdminVehicleController');
});
If I add 'name' => 'admin' to the Route::group() array, the route names will be adminvehicle.index and not admin.vehicle.index.
What is the correct way to combine all these parameters in the route?
Try to use as parameter for your admin group
Route::group(['prefix' => 'admin', 'middleware' => 'is.admin', 'as'=> 'admin.'], function () {
Route::get('/', 'AdminDashboardController#index')->name('dashboard');
Route::resource('vehicle', 'AdminVehicleController');
});
Reference Link
Supply a names array as part of the third parameter $options array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.
Route::resource('vehicle', 'AdminVehicleController', [
'names' => [
'index' => 'admin.vehicle.index',
// etc...
]
]);

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

Named Routes Conflict Laravel 5.2

I am creating a Multilingual Laravel 5.2 application and I am trying to figure out how to change language when I already have the content.
routes.php
...
Route::get('home', [
'as' => 'index',
'uses' => 'SiteController#home'
]);
Route::get([
'as' => 'index',
'uses' => 'SiteController#inicio'
]);
...
I have SiteController#home and SiteController#inicio. So I change session('language') in SiteController#change_language like:
...
public function change_language ($lang){
session(['language' => $lang]);
return redirect()->action(SAME NAMED ROUTE, DIFFERENT LANGUAGE);
}
...
So, When I click on a button with
English
from /inicio (SiteController#inicio) I should be redirected to the same named route (SiteController#home) so I can check the language and display appropriate content.
Any ideas of how to get the named route or something helpful?
Thank you :)

Laravel AJAX request, Method not allowed

I have a problem when trying to upload an image via bluimp's jQueryFileUpload.
In my routes i have this: Route::post('image/upload/{folder}', 'ImageController#upload');
my file input that is outside the <form> tags because it is independent to the form:
<input id="imageupload" type="file" name="image" multiple="" data-url="{{ url('admin/image/upload/members') }}" >
my jQuery function points to the data-url attribute value.:
$('#imageupload').fileupload({
dataType: 'json',
maxFileSize: 5000000,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
done: function (e, data) {
Members.handle_image(data);
}
});
The weird thing is that when i call this method from example.app/admin/members/create it works, but when i'm trying to access it from example.app/admin/members/1/edit i get a 405, Method not allowed.
In both cases, the Method is POST.
My routes for create and edit URIs:
Route::get('members/create', [
'uses' => 'MembersController#create', 'as' => 'admin/members/create'
]);
Route::get('members/{member}/edit', [
'uses' => 'MembersController#edit', 'as' => 'admin/members/edit'
]);
I'm sure is something really stupid that i can't see.
PS. I have a Project resource, where i also upload images, using the same route and function. It works on both cases (create and edit).
Anybody had this problem ?
Thank you!
Ok, i managed to solve this, but really i don't understand why it was not working.
In my routes i have this, where the ajax url points as POST:
Route::post('image/upload/{folder}', 'ImageController#upload');
This did not work.
I changed it to:
Route::any('image/upload/{folder}', 'ImageController#upload');
And now it works.
It is strange because on my request headers i have POST method, but with post (in routes) i did not work.
HTTP 405 indicates that the request method is not supported.
Both of your routes listen for get requests
Route::get('members/create', [
'uses' => 'MembersController#create', 'as' => 'admin/members/create'
]);
Route::get('members/{member}/edit', [
'uses' => 'MembersController#edit', 'as' => 'admin/members/edit'
]);
are you sure that you don't want one, or both to be post?
Route::post('members/create', [
'uses' => 'MembersController#create', 'as' => 'admin/members/create'
]);
Route::post('members/{member}/edit', [
'uses' => 'MembersController#edit', 'as' => 'admin/members/edit'
]);

Laravel - Get route name in filter

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

Categories