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
Related
I need to disable my register route in Laravel 8. Tried
Auth::routes([
'register' => false,
'login' => false,
]);
but the application threw up an error.
RuntimeException
In order to use the Auth::routes() method, please install the laravel/ui package.
If anyone points out what needs to change, will be grateful.
Thanks
Laravel 8 uses fortify authentication. To disable registration from your laravel app, you need to disable it from fortify, which is located on /config/fortify.php
'features' => [
// Features::registration(), // disable here
Features::resetPasswords(),
Features::emailVerification(),
Features::updateProfileInformation(),
Features::updatePasswords(),
Features::twoFactorAuthentication(),
],
At the end of my routes/web.php was the following line:
require __DIR__.'/auth.php';
In routes/auth.php are listed all additional routes for user login/register/logout. Just comment out or remove /register route from there.
In addition, be sure to disable related routes, in routes/web.php :
Route::get('/register', function() {
return redirect('/login');
});
Route::post('/register', function() {
return redirect('/login');
});
I changed according feature tests in tests/Feature/RegistrationTest.php to try to keep work clean so I needed those redirections.
Remove the registration routes from config/auth.php and then create a config/fortify.php (paste the content from: vendor/laravel/fortify/config/fortify.php) which will override the default settings.
Inside config/fortify.php comment out the first element of features array (Features::registration()) then run php artisan optimize to clear config cache and routes cache.
Now all your removed routes should return 404, you can also check if those still exist with php artisan route:list
config/fortify.php:
<?php
use Laravel\Fortify\Features;
return [
'guard' => 'web',
'middleware' => ['web'],
'passwords' => 'users',
'username' => 'email',
'email' => 'email',
'views' => true,
'home' => '/home',
'prefix' => '',
'domain' => null,
'limiters' => [
'login' => null,
],
'features' => [
//Features::registration(),
Features::resetPasswords(),
Features::emailVerification(),
Features::updateProfileInformation(),
Features::updatePasswords(),
Features::twoFactorAuthentication(),
],
];
Just use:
Auth::routes(['register' => false]);
Do not break your head with different versions of packages and Laravel. Because maybe you don't have fortify.php in your config, or using different packages. All routes are in routes/web now. Just go there and force that '/register' sends to login or any other view you want to:
Route::any('/register', function() {
return view('auth.login');
});
That way you maintain out of reach that feature, but close for when you need it.
Remove this code from routes/auth.php
Route::get('/register', [RegisteredUserController::class, 'create'])
->middleware('guest')
->name('register');
Route::post('/register', [RegisteredUserController::class, 'store'])
->middleware('guest');
Just put this in your /routes/web.php:
Route::any('/register', [App\Http\Controllers\HomeController::class, 'index']);
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}.
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
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 :)
I am struggling to access a route prefix parameter from my middleware.
Given this URL: http://www.example.com/api/v1/campaign/40/status, and the following route:
Route::group( [
'prefix' => 'api/v1'
], function()
{
Route::group( [
'prefix' => 'campaign/{campaign}',
'where' => [ 'campaign' => '[0-9]+' ],
'middleware' => [
'inject_campaign'
]
], function()
{
Route::get( 'status', 'CampaignController#getStatus' );
} );
} );
How do I access the campaign parameter (40 in the example URL) from my inject_campaign middleware? I have the middleware running fine but cannot work out how to access the parameter.
Calling $request->segments() in my middleware gives me the parts of the route but this seems a fragile way of accessing the data. What if the route changes?
You can do it using shorter syntax
You can use:
echo $request->route()->campaign;
or even shorter:
echo $request->campaign;
Got it!
$request->route()->getParameter('campaign')