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}.
Related
I try to use the following package in Laravel 8: https://github.com/kristijanhusak/laravel-form-builder
In the documentation it says the routes should look like this:
Route::get('songs/create', [
'uses' => 'SongsController#create',
'as' => 'song.create'
]);
Route::post('songs', [
'uses' => 'SongsController#store',
'as' => 'song.store'
]);
Which does not work for Laravel 8, so i changed the code according to the following post:https://stackoverflow.com/a/63808132/2192013
Route::get('songs/create', [
SongsController::class, 'create'
]);
Route::post('songs', [
SongsController::class, 'store'
]);
But now when i go to /songs/create i get the following error:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [song.store] not defined.
How can i make it work in Laravel 8, which should be supported by the package?
the error you get says that song.store is not defined which is right cause you had given it a name and now you dont according to the given code. Try this instead.
Route::get('songs/create', [
SongsController::class, 'create'
])->name('song.create');
Route::post('songs', [
SongsController::class, 'store'
])->name('song.store');
in your form you probably use the named route song.store
I really need help!
On the Laravel 5.3, it seems that a group of routes into an another group isn't working for me. Is there any solution to make it work ? Or do I have to write my routes in another way ? This is the way I write my routes:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]
],
function()
{
Route::group([
'prefix' => 'admin'
], function () {
// General Settings
Route::get('settings', [
'as' => 'admin.settings',
'uses' => 'AdminController#showSettings'
]);
});
});
I am using a prefix in the first group for the multiple language and the another group is for the admin section. I want an url like this : /en/admin/settings
The problem is solved! It did not work because I had used another route in which there was an paramater before the group of admin section ex:
Route::get('activate/{code}', [
'as' => 'auth.activation',
'uses' => 'RegistrationController#getActivate'
]);
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.
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 :)
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