Route to grouped routes with prefix correctly in Laravel - laravel

I have a group of routes with a prefix.
In my web routes the routes with an admin prefix go to a separate route file:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'as' => 'admin.', 'middleware' => 'admin'], function () {
includeRouteFiles(__DIR__ . '/Admin/');
});
So I have to add admin as prefix in my routes. In my Admin directory I defined the routes as following:
Route::prefix('organization/{organization}')->group(function () {
Route::post('seed', 'SeedController#store')->name('seed');
});
My problem is routing to the routes inside this group. I used the command php artisan route:list to see more info about my routes. It says:
My route name is: admin.seed
My URI is: admin/organization/{organization}/seed
When I link to this route as admin.seed in my form I get the following error:
Missing required parameters for [Route: admin.seed] [URI:
admin/organization/{organization}/seed]. (View:
D:\xampp\htdocs\minute-mn-503\resources\views\admin\organizations\show.blade.php)
I tried linking it as:
admin.seed
admin/seed/1
admin/organization/1/seed
admin/organization/1.seed
But none of them seems to work. This is the line of code for example:
<form method="POST" action="{{ route('admin/organization/'.$organization->id.'.seed') }}">
Any idea on how I might route these correctly? I couldn't find any clear explanation in the Laravel docs.

You must be using this:
<form method="POST" action="{{ url('admin/organization/' . $organization->id .'/seed') }}">
or:
<form method="POST" action="{{ route('admin.seed', $organization->id) }}">

Related

Laravel 6 - Named route in blade not allowing special characters or capital letters in parameters

I've recently upgraded to laravel 6 and I've noticed an odd bug. Some of my routes in my blade templates stopped working.
Take this route for example:
Route::post('/create-save-folder/', [
'uses' => 'SaveFolderController#createSaveFolder',
'as' => 'create.save.folder',
'middleware' => ['auth'],
]);
In blade this used to work just fine back in laravel 5.8:
<a href="{{ route('get.save.folder', ['ID' => $folder->ID, 'URL_title' => $folder->URL_title]) }}">
However now it gives me the error:
Missing required parameters for [Route: get.save.folder] [URI:
save-folder/{ID}/{URL_title}]. (View:
C:\xampp\htdocs\MC\resources\views\partials\user_sidebar_block.blade.php)
So I did some debugging. If I change the parameters to random strings, like so:
<a href="{{ route('get.save.folder', ['ID' => 'test', 'URL_title' => 'test']) }}">
that works fine.
So after more debugging I tried changing '$folder->ID' to '$folder->id' in the first parameter.
That ended up working. Which is really weird because if I write something like this in blade:
<p>URL TITLE:{{$folder->URL_title}}</p>
<p>ID:{{$folder->ID}}</p>
it will return the proper results. So that works fine for 'id', but 'URL_title' is still giving me trouble because it has an underscore. So unless I switch my database column to 'urltitle' instead of 'URL_title', I don't know how I'm going to get this route to work.
Why is this happening?
I also had some problems with my blade files when upgrading but not exactly the same as yours.
Doing php artisan view:clear did the trick for me.

Laravel5.7 routing using Route:match not working

I'm using Laravel 5.7 I'm trying to route my function for get and post.
I want to load a view and post a form.
As I have studied
Route::match(['GET','POST'], '/', TestController#test);
Route::any('/', TestController#test);`
one of these should work.
But its not working for me,is there any other way or I'm doing something wrong?
UPDATE
Route to admin:
Route::match(['get','post'], 'cp/', 'AdminController#test');
Function in Admin controller:
public function test( Request $request){
$data=array();
if ($request->isMethod('POST')) {
echo "here it is";
exit;
}else{
echo "still in get!";
}
return view('admin/login', $data);
}
And my view in short is something like this:
<form action="{{ url('/cp') }}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<form>
Can you try changing
Route::match(['GET','POST'], '/', TestController#test);
to
Route::match(['GET','POST'], '/', 'TestController#test');
OR
Route::any('/', TestController#test);
to
Route::any('/', 'TestController#test');
The second param should be wrapped in quotes!
UPDATE:
Your route match code should be something like this :
Route::match(array('GET', 'POST', 'PUT'), "/", array(
'uses' => 'Controller#index',
'as' => 'index'
));
Try this in you web.php
Route::match(['get', 'post'], '/testMethods', function ()
{
dd('its workong bro');
});
And hit the yourprojectname/testMethods in your web browser
Eg: http://localhost:8000/testMethods
From Illuminate\Contracts\Routing\Registrar.php
public function match($methods, $uri, $action);
Here is match function parameter list
Parameter One List of methods: Eg: get,post,put,patch
Parameter two url : Eg: /testMethods
Parameter three Method: Eg: TestController#test
Route::match(['get', 'post'], '/testMethods','TestController#test');
Well, by the end what I understand to use route::match I should specify function name without it it will not work.So when I changed it to Route::match(array('GET', 'POST', 'PUT'), "/login", array(
'uses' => 'AdminController#login',
'as' => 'login'
));
It resolves the issue. Thank for the help everyone!!

Laravel 5.2 Auth Registration Redirect

I am trying to wrap the default Auth in /en/wholesale/login, but I am unable to submit a registration form. What file should I touch? currently it will just refresh the page. instead of heading to RegistersUsers.php / register(Request $request).
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'web' ]
],
function()
{
Route::group(['prefix' => 'wholesale'], function () {
// Authentication Routes...
Route::get('login', 'Auth\AuthController#showLoginForm');
Route::post('login', 'Auth\AuthController#login');
Route::get('logout', 'Auth\AuthController#logout');
// Registration Routes...
Route::get('register', 'Auth\AuthController#showRegistrationForm');
Route::post('register', 'Auth\AuthController#register');
// Password Reset Routes...
Route::get('password/reset/{token?}', 'Auth\PasswordController#showResetForm');
Route::post('password/email', 'Auth\PasswordController#sendResetLinkEmail');
Route::post('password/reset', 'Auth\PasswordController#reset');
Route::get('/', 'HomeController#index');
Route::get('/', 'HomeController#index');
});
});
blade
<form class="form-horizontal" role="form" method="POST" action="{{ url('wholesale/login') }}">
EDIT
I believe it have to do with
Route::post('register', 'Auth\AuthController#register');
the app will still use the GET method instead of post on form submit.
EDIT 2
ok I solve the part of the question
action="{{ url('en/wholesale/register') }}"
I need to hardcode the 'en' part, in-order to route to the function, is there way that part can be route automatically?
if you use default authentication from Laravel you can see router.php file as all login and registration route are registered there and you can call them in your routes.php file with Route::auth(). you might want to see AuthenticatesUsers or RegistersUsers traits as authController.php use AuthenticatesAndRegistersUsers trait to config your authentication. hope it helps.

Do i have to define every controller method in routes.php?

This is my current routes.php file:
<?php
Route::get('/', 'AdminController#index');
Route::get('/posts','PostsController#index');
Route::get('/posts/create','PostsController#create');
Route::get('/tags','TagsController#index');
Route::get('/health','HealthController#index');
Route::get('/health/create','HealthController#create');
Route::get('/health/categories','HealthController#categories');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController'
]);
If i dont do that for exmaple, i canbt use helpers in blade views , like action. It throws a null exception.
So the question is, do i have to define here all the controller actions? Or else i will not be able to use them directly? For example in a redirect to action link.
Adding this to blade:
<a href="{{ action('PostsController#index') }}">
throws an exception UNLESS i specifically add the route with Route::get NOT working if i add an entry to Route::controllers.
Tried also
<a href="{{ action('\App\Http\Controllers\PostsController#getIndex') }}">
<a href="{{ action('\App\Http\Controllers\PostsController#index') }}">
The problem here are your controller action names. If you use implicit controller routes (Route::controllers) your method names have to start with an HTTP verb.
Instead of index() you need getIndex().
You can easily check what routes Laravel actually registers using the php artisan route:list command.
Generating an URL would then look like this:
<a href="{{ action('PostsController#getIndex') }}">
Note Controller routing only works if you add PostsController to the Route::controllers. (I assumed you did so but just to be sure)
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
'posts' => `PostsController`
]);

Laravel: Named route not found

I have the following named route in routes.php:
Route::get('/', 'DefaultController#index', array('as' => 'home'));
Then, I try to get a link to the route in another view (404.blade.php), based on docs:
Go to homepage
However, the page with the code above thows this: Error in exception handler: Route [home] not defined. I tried using simply route('home') as well.
What am I missing?
The syntax for named routes is a bit different
Route::get('/', array('as' => 'home', 'uses' => 'DefaultController#index'));

Resources