Laravel5.7 routing using Route:match not working - laravel

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!!

Related

Add locale to laravel resource route

I have following route that expect locale in prefix
Route::group(['prefix' => '{locale}/admin', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function() {
Route::resource('/profile', 'Admin\ProfileController', ['except' => ['edit', 'create', 'destroy', 'show', 'store']]);
});
The final route supposed to be like this:
www.example.com/en/admin/profile
and
www.example.com/en/admin/profile/1
while I can get index route www.example.com/en/admin/profile, but I cannot get update route www.example.com/en/admin/profile/1 instead I have this
www.example.com/1/admin/profile/en
Here is how my routes are defined in blade
// Return index route correctly
{{route('profile.index', app()->getLocale())}}
//And
{{route('profile.update', [$user->id, app()->getLocale()])}}
// Return update route wrong as I mentioned above.
My question is: How should I define my update route to return correct url?
You are passing your parameters in wrong order. You need to send locale first and $user->id after that.
{{ route('profile.update', [app()->getLocale(), $user->id]) }}

Laravel wrong bind of update method with Request param

I've a problem with routing calling a "update" (PUT) method from a form with Laravel.
Routing doesn't work if I put Illuminate\Http\Request param in "update" because method "show" method is called instead, even if 'method' => 'put' is present in the form.
Removing Request param everything works fine.
Below my code.
web.php
Route::resource('profile', 'ProfileController');
ProfileController.php
public function update(Request $request, $id)
{
...
}
public function show($id)
{
...
}
index.blade.php
{!! Form::model($profile, array('action' => array('ProfileController#update', $profile->id), 'method' => 'put', 'class' => 'form-horizontal')) !!}
Could you help me to understand where what's wrong?
Thanks in advance.

I can't insert data in database laravel 5.2

I am trying to insert data in my database.But I can't to do it. I try my best. I know its a simple question.
Here is my route:
Route::post('/class', [
'uses' => 'classroom#showdata',
'as' => 'classrooms']);
Here is my Controller:
public function showclass(Request $request)
{
$randomnumber = rand(50001,1000000);
$classrooms = new Classrooms();
$classrooms->class_name = $request['class_name'];
$classrooms->subject_name = $request['subject_name'];
$classrooms->section = $request['section'];
$classrooms->class_code = $randomnumber;
$classrooms -> user_id = Auth::user()->id;
$classrooms -> save();
return view('class', array('classroom' => Auth::user()) );
}
Here is my form:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/class') }}">
{!! csrf_field() !!}
Where is my mistake?
There is an error in your Route. You have used showdata method instead of showclass. Just change it like below:
Route::post('/class', [
'uses' => 'classroom#showclass',
'as' => 'classrooms']);
Note: Make sure to specify specific method while defining the routes.

Passing a param containing a path to a controller

I'm trying to pass a variable who contains a path from a form to a controller function in Laravel 4, with the purpose of download an image. I tried a lot of things but nothing worked for me. If I don't pass the parameter in the route, I get a missing parameter error, and if I pass the parameter in the route, I get a NotFoundHttpException.
Here's my code:
View Form:
{{ Form::open(array('route' => array('download', $myPath))) }}
{{ Form::submit('Download', array('class' => 'generator')); }}
{{ Form::close() }}
Route:
Route::post('/download/{myPath}', array('uses' => 'ImagesController#download', 'as' => 'download'));
ImagesController function:
public function download($myPath){
return Response::download($myPath);
}
When I click the submit I'm obtaining a URL like this with NotFoundHttpException:
http://localhost/resizer/public/download/images/myimage.jpg
I don't understand what I'm missing.
You are actually posting two variables. Try this instead
Route::post('/download/{myFolder}/{myFile}', array('uses' => 'ImagesController#download', 'as' => 'download'));
and in your controller
public function download($myFolder, $myFile){
return Response::download($myFolder.'/'.$myFile);
}

Laravel 4 password reminder: redirection issue

I'm using the Laravel 4 password reminder functionality, as described here: http://four.laravel.com/docs/security#password-reminders-and-reset. In order to generate the token, send the email and create de DB record in the password_reminder table, I use the standard code in my routes file :
Route::post('password/remind', function() {
$credentials = array('email' => Input::get('email'));
return Password::remind($credentials);
});
This code is suppose to send me back to my input form in case of any error (unknown email address for instance). Instead of that, I get a MethodNotAllowedHttpException. The reason is Laravel don't try to send me back to my form URL (which is /password/forgot): he tries to redirect me to /password/remind, in GET, and this route does not exist (of course) in my routes.php file.
I checked the code of the Illuminate\Auth\Reminders\PasswordBroker class, which is responsible of this redirection, and found out this method :
protected function makeErrorRedirect($reason = '')
{
if ($reason != '') $reason = 'reminders.'.$reason;
return $this->redirect->refresh()->with('error', true)->with('reason', $reason);
}
I replaced $this->redirect->refresh() by $this->redirect->back(), and everything is now working as excepted. But as I couldn't find any comment on this bug anywhere, I assume I'm doing something wrong… But I can't find what !
Here is my routes.php file:
Route::get('password/forgot', array('as' => 'forgot', 'uses' => 'SessionsController#forgot'));
Route::post('password/remind', function() {
$credentials = array('email' => Input::get('email'));
return Password::remind($credentials);
});
Route::get('password/reset/{token}', function($token) {
return View::make('sessions.reset')->with('token', $token);
});
Route::post('password/reset/{token}', array('as' => 'reset', 'uses' => 'SessionsController#reset'));
my SessionsController relevant code:
class SessionsController extends BaseController {
[...]
public function forgot() {
return View::make('sessions.forgot');
}
public function reset() {
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'password_confirmation' => Input::get('password_confirmation')
);
Input::flash();
return Password::reset($credentials, function($user, $password) {
$user->password = Hash::make($password);
$user->save();
return Redirect::to('home');
});
}
}
and finally my view code:
{{ Form::open(array('url' => 'password/remind', 'class' => 'form', 'role' => 'form', 'method'=>'post')) }}
<div class="form-group">
{{ Form::label('email', 'E-mail') }}
{{ Form::text('email', '', array('autocomplete'=>'off', 'class' => 'form-control')) }}
</div>
{{ Form::submit("Envoyer", array("class"=>"btn btn-primary")) }}
{{ Form::close() }}
If it is possible, I highly recommend you to upgrade to Laravel 4.1, because it comes with a more flexible (also easier to understand and to work with) solution for password remind/reset.
Check out this example with Laravel 4.1:
https://github.com/laracasts/Laravel-4.1-Password-Resets/blob/master/app/controllers/RemindersController.php

Resources