I am new to Laravel, I was trying to use a form and thus used post method in it after adding CSRF Token. Also I have downloaded form builder package and have made all the adjustments needed for the same. But somehow, the post method in the routes is not working. Although I tried using get and any method in routes and they worked perfect fine.
Controller:
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
public function formSubmit()
{
echo "Form Submit Method";
return view('layouts\formView');
}
}
View:
#extends('layouts\masterLayout')
#section('head')
{!!Form::open(['route'=>'f.submit'])!!}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{!!Form::text('field-one')!!}
{!!Form::submit('')!!}
#endsection
Route:
Route::post('form-submit',['uses'=>'Admin\AdminController#formSubmit', 'as'=>'f.submit']);
Error: Stack Trace
in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 821
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53
Change
{!!Form::open(['route'=>'f.submit'])!!}
to
{!!Form::open(['route'=>'f.submit', 'method' => 'POST'])!!}
Reason is probably obvious ;-)
EDIT
Can you change the signature of the formSubmit function to this:
public function formSubmit(Request $request)
{
echo "Form Submit Method";
return view('layouts\formView');
}
Instead of
Use as follow that will create Input field with unique token
{{csrf_token()}}
Change Route To
Route::post('form-submit',[ 'as'=>'f.submit','uses'=>'Admin\AdminController#formSubmit' ]);
change View Method
return view('layouts\formView');
Like
return view('layouts.formView');
For E.g
return view('YourFolderName.FileName');
I already had a look at other post about how to fix the ReflectionException issue in laravel Lumen, using this:
$request = Illuminate\Http\Request::capture();
$app->run($request);
However it is not solving my problem. I have a controller called AccountController.php and placed in app/Http/Controllers/Account folder and here is the code:
<?php
namespace App\Http\Controllers\Account;
use App\Account;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AccountController extends Controller {
public function createNewAccount(Request $request) {
$newAccount = Account::create($request->all());
return response()->json($newAccount);
}
}
And this is my route file placed in /routes/web.php:
<?php
$app->get('/hello', function () use ($app) {
return 'Hello World!';
});
$app->group(['prefix' => 'api/v1','namespace' => 'App\Http\Controllers\Account'], function($app)
{
$app->post('account','AccountController#createNewAccount');
});
When I test with Postman the get request which returns a simple 'Hello World' is working fine, but the POST call to api/v1/account/createNewAccount will always fail whatever I do:
ReflectionException in Container.php line 681:
Class App\Http\Controllers\App\Http\Controllers\Account\AccountController does not exist
in Container.php line 681
at ReflectionClass->__construct('App\Http\Controllers\App\Http\Controllers\Account\AccountController') in Container.php line 681
at Container->build('App\Http\Controllers\App\Http\Controllers\Account\AccountController') in Container.php line 565
at Container->make('App\Http\Controllers\App\Http\Controllers\Account\AccountController') in Application.php line 208
at Application->make('App\Http\Controllers\App\Http\Controllers\Account\AccountController') in RoutesRequests.php line 677
at Application->callControllerAction(array(true, array('uses' => 'App\Http\Controllers\App\Http\Controllers\Account\AccountController#createNewAccount'), array())) in RoutesRequests.php line 644
at Application->callActionOnArrayBasedRoute(array(true, array('uses' => 'App\Http\Controllers\App\Http\Controllers\Account\AccountController#createNewAccount'), array())) in RoutesRequests.php line 629
at Application->handleFoundRoute(array(true, array('uses' => 'App\Http\Controllers\App\Http\Controllers\Account\AccountController#createNewAccount'), array())) in RoutesRequests.php line 528
at Application->Laravel\Lumen\Concerns{closure}() in RoutesRequests.php line 782
at Application->sendThroughPipeline(array(), object(Closure)) in RoutesRequests.php line 534
at Application->dispatch(object(Request)) in RoutesRequests.php line 475
at Application->run(object(Request)) in index.php line 29
I am using "laravel/lumen-framework": "5.4.*".
There is no reply to this particular issue, I decided to build my API with Dingo API: https://github.com/dingo/api It is a good package to build API with Laravel/lumen. They created their own routing system and things are going much better since.
When a new user registers to my app, I'm trying to create new row in the registrations table that contains a timestamp column.
I get the following error:
ErrorException in Registration.php line 111: Trying to get property of non-object
in Registration.php line 111
at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/app/Registration.php', '111', array('registration_time' => object(Carbon))) in Registration.php line 111
at Registration->setRegistrationTimeAttribute(object(Carbon)) in Model.php line 2863
at Model->setAttribute('registration_time', object(Carbon)) in Model.php line 422
AuthController.php (controller)
$registration = Registration::create([
'business_id' => '1',
'registration_time' => \Carbon\Carbon::today()->addDays(3)->addHours(10)->toDateTimeString()
]);
Echo of registration_time in controller
2016-02-12 10:00:00
Registration.php (model) line 111
public function setRegistrationTimeAttribute($registration_time)
{
$this->attributes['registration_time'] = Carbon::createFromFormat('Y-m-d H:i', $registration_time, \Auth::user()->timezone)->setTimezone('UTC');
}
What I don't understand, is that similar code works in my RegistrationTableSeeder, my model accepts this format.
'registration_time' => \Carbon\Carbon::now()->addDays(3)->addHours(1)->toDateTimeString(),
I am getting ServerException in RequestException.php line 107 when I make POST request to my API. I get following error -
Server error: POST http://10.10.1.40:3000/auth/register resulted in a 500 Internal Server Error response:
{"statusCode":500,"errorMessage":"[object Object]"}.
I tried sending post request from REST Client and it works.
Following is the trace
in RequestException.php line 107 at RequestException::create(object(Request), object(Response)) in Middleware.php line 65
at Middleware::GuzzleHttp\{closure}(object(Response)) in Promise.php line 199
at Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null)) in Promise.php line 152
at Promise::GuzzleHttp\Promise\{closure}() in TaskQueue.php line 60
at TaskQueue->run(true) in Promise.php line 240
at Client->request('POST', 'http://10.10.1.40:3000/auth/register', array('body' => '{"firstName":"abc","lastName":"ab"}')) in RegisterController.php line 30
at RegisterController->postRegisterForm(object(Request))
Following is my controller code
class RegisterController extends Controller{
public function postRegisterForm(Request $request){
$jsonData = json_encode($_POST);
$client = new Client();
$res = $client->request('POST','10.10.1.40:3000/auth/register', ['body' => $jsonData]);
echo $res->getStatusCode();
echo $res->getBody();
}
}
Any suggestions?
You are right #ShaunBramley. I missed out sending the content type header in request. Adding following code works -
$headers = ['Content-Type' => 'application/json'];
$res = $client->request('POST', 'http://10.10.1.40:3000/auth/register', ['headers'=>$headers,'body' => $jsonData]);
I've successfully implemented Laravel's Auth for Registration, Login and Logout. However, I'm having quite a bit of difficulty resolving the following error trying to implement the password reset functionality.
Error: User must implement CanResetPassword interface.
I believe my User class is configured properly:
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
CanResetPasswordContract {
use Authenticatable, CanResetPassword;
...
}
I'm not using Eloquent, so I have the following in config/auth.php:
'driver' => 'database'
Does anyone have any idea why I'm getting the "User must implement CanResetPassword interface" error?
Thanks much!
EDIT: Adding Stack Trace:
UnexpectedValueException in PasswordBroker.php line 237:
User must implement CanResetPassword interface .
in PasswordBroker.php line 237
at PasswordBroker->getUser( array('email' => 'admin2#beaudini.com')) in PasswordBroker.php line 81
at PasswordBroker->sendResetLink (array('email' => 'admin2#beaudini.com'), object(Closure)) in Facade .php line 217
at Facade::__callStatic('sendResetLink' , array(array('email' => 'admin2#beaudini.com'), object(Closure))) in ResetsPasswords.php line 35
at Password::sendResetLink(array ('email' => 'admin2#beaudini.com'), object(Closure)) in ResetsPasswords .php line 35
at PasswordController->postEmail (object(Request))
at call_user_func_array(array(object(PasswordController), 'postEmail'), array( object(Request))) in Controller.php line 256
at Controller->callAction('postEmail', array(object(Request))) in ControllerDispatcher .php line 164
at ControllerDispatcher->call (object(PasswordController), object(Route), 'postEmail') in ControllerDispatcher .php line 112
at ControllerDispatcher->Illuminate \Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in RedirectIfAuthenticated.php line 41
at RedirectIfAuthenticated->handle(object(Request), object(Closure))
at call_user_func_array(array(object(RedirectIfAuthenticated), 'handle'), array (object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack (object(PasswordController), object(Route), object(Request), 'postEmail') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch (object(Route), object(Request), 'App\Http\Controllers\Auth\PasswordController', 'postEmail') in Route.php line 203
at Route->runWithCustomDispatcher(object (Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 704
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure }(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object< /em>(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object (Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http \{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in Debugbar.php line 49
at Debugbar->handle(object (Request), object(Closure ))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object (Request), object(Closure ))) in Pipeline .php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in ShareErrorsFromSession .php line 49
at ShareErrorsFromSession ->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in StartSession.php line 62< /a>
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline .php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in AddQueuedCookiesToResponse .php line 37
at AddQueuedCookiesToResponse ->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array (object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle (object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline .php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request)) in CheckForMaintenanceMode .php line 42
at CheckForMaintenanceMode ->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure }(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter (object(Request)) in Kernel.php line 87
at Kernel->handle(object( Request)) in index.php line 54
I faced this problem in laravel 5.2, and I found a solution like as:
<?php
namespace App;
use Eloquent;
// use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\CanResetPassword as
CanResetPasswordContract;
class User extends Eloquent implements
Authenticatable,CanResetPasswordContract
{
use AuthenticableTrait,CanResetPassword;
.......
.......
}
Follow your stack trace.
PasswordBroker.php line 237
https://github.com/laravel/framework/blob/5.1/src/Illuminate/Auth/Passwords/PasswordBroker.php#L237
Fails because DatabaseUserProvider's retrieveByCredentials returns a GenericUser object, not your User class object.
https://github.com/laravel/framework/blob/5.1/src/Illuminate/Auth/DatabaseUserProvider.php#L99
What this means, basically, is that the password reminder functionality that Laravel comes with is only supported by the eloquent auth driver. You can't use it with the database driver. Your options are
switch to Eloquent
build out password resets by yourself, or
add your own custom auth driver
Inside of a service provider (AppServiceProvider, or create a new one):
Auth::extend('custom', function($app) {
return new CustomUserProvider($app['database.connection']);
});
It might be as simple as extending the database user provider and overriding getGenericUser to use your class instead.
class CustomUserProvider extends \Illuminate\Auth\DatabaseUserProvider
{
protected function getGenericUser($user) {
if ($user !== null) {
return new \App\User((array) $user);
}
}
}
The below should work in Any Model You Created
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class customer extends Model implements AuthenticatableContract,CanResetPasswordContract
{
//protected $primaryKey = 'customerId';
use AuthenticableTrait,CanResetPassword;
use ValidatesRequests;
protected $fillable=['name','email','password','country','favorite','gender','image'];
protected $hidden = [
'password', 'remember_token',
];
}
hope it helps