I am using Laravel 5.4,
I rewrite the validator() method of RegisterController out of the box,as follow:
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
protected function validator(array $data)
{
$validationCode = Request::session()->get('validation_code', '');
return Validator::make($data, [
'name' => 'required|max:255',
'role' => 'required|in:1,2',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'validation_code' => 'required|in:' . $validationCode
]);
}
}
There is an error:
Non-static method Illuminate\Http\Request::session() should not be called statically
Why is it?
Change this:
$validationCode = Request::session()->get('validation_code', '');
to this:
$validationCode = session()->get('validation_code', '');
//or
$validationCode = request()->session()->get('validation_code', '');
//or
$validationCode = Illuminate\Support\Facades\Request::session()->get('validation_code', '');
//or
$validationCode = \Request::session()->get('validation_code', '');
Illuminate\Support\Facades\Request and Illuminate\Http\Request are two different class, the first is the facade the second the actual request class. My advice is to use the helper function request() you will have less confusion.
Related
I would like to save the information that I am receiving in the response of a request, in this case the "access_token" field, to my mysql database, here is the code:
My controller,
here I make a post request to have the access token:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class AuthsController extends Controller
{
public function SocialAuth(Request $request)
{
$a = $request->input('auth_code');
// URL
$apiURL = 'https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/';
// POST Data
$postInput = [
'app_id' => '7112335319877287',
'secret' => '18f52730856f43ed821187bfa9283794ca360e',
'auth_code' => $a
];
// Headers
$headers = [
//...
];
$response = Http::withHeaders($headers)->post($apiURL, $postInput);
$statusCode = $response->getStatusCode();
$responseBody = json_decode($response->getBody(), true);
echo $statusCode; // status code
dd($responseBody); // body response
}
}
Response of my request, the value that I want to save to mysql is the access token
^ array:4 [▼
"code" => 0
"message" => "OK"
"request_id" => "202211281314430102451411010AF4AA0A"
"data" => array:3 [▼
"access_token" => "fbcaa610339b7aeb39eabf29346d06a4e7fe9"
"advertiser_ids" => array:1 [▶]
"scope" => array:18 [▶]
]
]
How can I save the access token in mysql?
create a table with the following columns, for storage:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTokenTableTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('token_table', function (Blueprint $table) {
$table->integer('id_token')->primary();
$table->string('token')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('token_table');
}
}
Use your token Model and save the data
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
// call your token Model class
use App\Models\TokenTable
class AuthsController extends Controller
{
public function SocialAuth(Request $request)
{
$a = $request->input('auth_code');
// URL
$apiURL = 'https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/';
// POST Data
$postInput = [
'app_id' => '7112335319877287',
'secret' => '18f52730856f43ed821187bfa9283794ca360e',
'auth_code' => $a
];
// Headers
$headers = [
//...
];
$response = Http::withHeaders($headers)->post($apiURL, $postInput);
$statusCode = $response->getStatusCode();
$responseBody = json_decode($response->getBody(), true);
echo $statusCode; // status code
//check if status code is 200
if($statusCode == 200){
TokenTable::create([
'token' => $responseBody['data']->access_token
]);
echo 'ok';
}
}
}
or this
if($statusCode == 200){
TokenTable::create([
'token' => $responseBody['data']['access_token']
]);
echo 'ok';
}
In an ActiveDataProvider you can use closures as values, like:
$dataprovider = new ArrayDataProvider([
'allModels' => $array
]);
$gridColumns = [
'attrib_1',
[
'attribute' => 'attrib_2',
'label' => 'Label_2',
'value' => function($model) {
return Html::encode($model->value_2);
}
],
'attrib_3'
];
echo GridView::widget([
'dataProvider'=> $dataprovider,
'columns' => $gridColumns
]);
Is it possible to do the same or something like this, in an ArrayDataProvider?
Yes. Only difference is that $model is not an object but array so:
'value' => function($model) {
return Html::encode($model['value_2']);
}
For this purpose, I have created an extended version of ActiveDataProvider, that for each model got from provider I call a callback.
This is the custom ActiveDataProvider, put in common\components namespace in this case.
<?php
namespace common\components;
class CustomActiveDataProvider extends \yii\data\ActiveDataProvider
{
public $formatModelOutput = null;
public function getModels()
{
$inputModels = parent::getModels();
$outputModels = [];
if($this->formatModelOutput != null)
{
for($k=0;$k<count($inputModels);$k++)
{
$outputModels[] = call_user_func( $this->formatModelOutput, $k , $inputModels[$k]);
}
}
else
{
$outputModels = $inputModels;
}
return $outputModels;
}
}
This is the action in controller that uses it. For reusability, I call a model method instead calling a clousure, but you can call also a clousure.
public function actionIndex()
{
$query = Model::find();
$dataProvider = new \common\components\CustomActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => null],
'formatModelOutput' => function($id, $model) {
return $model->dataModelPerActiveProvider;
}
]);
return $dataProvider;
}
At last, this is the method getDataModelPerActiveProvider in model:
public function getDataModelPerActiveProvider()
{
$this->id = 1;
// here you can customize other fields
// OR you can also return a custom array, for example:
// return ['field1' => 'test', 'field2' => 'foo', 'field3' => $this->id];
return $this;
}
I have a registration form with fields that are validated in User entity class. The validation works fine, however I can't return JsonResponse with form error messages in it.
My registration form controller method looks like this:
/**
* #Route("/register", name="register")
*/
public function registerAction(Request $request)
{
$user = new User();
$form = $this->createForm(RegistrationType::class, $user);
$form->handleRequest($request);
$errors = "";
if ($form->isSubmitted())
{
if ($form->isValid())
{
$password = $this->get('security.password_encoder')
->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
$user->setIsActive(1);
$user->setLastname('none');
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return new JsonResponse(
array(
'message' => 'Success! User registered!',
), 200);
}
else
{
$errors = ($this->get('validator')->validate($form));
return new JsonResponse(
array(
'message' => 'Not registered',
'errors' => $errors,
), 400);
}
}
return $this->render(
'ImmoBundle::Security/register.html.twig',
array('form' => $form->createView(), 'errors' => $errors)
);
}
I get the following json response when I submit the registration form with invalid data:
{"message":"Not registered","errors":{}}
Actually I'm expecting that "errors":{} will contain some error fields, but it doesn't. Does anyone know what the problem here is?
UPD:
My RegistrationType looks like this:
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname', TextType::class)
->add('email', EmailType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat password'),
'invalid_message' => "Passwords don't match!",
))
->add('register', SubmitType::class, array('label' => 'Register'));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ImmoBundle\Entity\User',
'csrf_protection' => true,
'csrf_field_name' => '_token',
'csrf_token_id' => 'authenticate',
));
}
}
UPD2: Found the solution. I needed to do this iteration and then call for getMessage():
$allErrors = ($this->get('validator')->validate($form));
foreach ($allErrors as $error)
{
$errors[] = $error->getMessage();
}
Form validated when you call $form->handleRequest($request);
To get form errors use getErrors method
$errors = $form->getErrors(true); // $errors will be Iterator
to convert errors object to messages array you can use code from this response - Handle form errors in controller and pass it to twig
This is exapmle how i'm process errors in one of my projects
$response = $this->get('http.response_formatter');
if (!$form->isValid()) {
$errors = $form->getErrors(true);
foreach ($errors as $error) {
$response->addError($error->getMessage(), Response::HTTP_BAD_REQUEST);
}
return $response->jsonResponse(Response::HTTP_BAD_REQUEST);
}
It's worked for me.
And also this can help you - Symfony2 : How to get form validation errors after binding the request to the form
You must set error_bubbling to true in your form type by explicitly setting the option for each and every field.
I am new for hooks in codeigniter. I have enabled the hooks in config file.
$config['enable_hooks'] = TRUE;
and then in hooks.php I have written my hook that is like below
$hook['pre_controller'] = array(
'class' => 'MyClass',
'function' => 'Myfunction',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => '');
AND the class having function is as below
class MyClass {
function MyClass() {
$this->CI = &get_instance();
require_once(APPPATH . 'config/database.php');
}
function Myfunction() {
$record = $this->CI->db->SELECT('*')
->FROM('currency')
->get()
->result();
echo "<pre>";
print_r($record);
die;
}}
but i am getting a blank page. please tell me what is wrong with me.
I think your main problem was in this area.
function MyClass() {
$this->CI = &get_instance();
require_once(APPPATH . 'config/database.php');
}
Try
Note: codeigniter 3 versions are case sensitive should be first letter upper case only on class and file name.
application > hooks > My_class.php
<?php
class My_class {
public function __construct() {
$this->CI = &get_instance();
// Auto load database
// require_once(APPPATH . 'config/database.php');
}
public function my_function() {
$query = $this->CI->db->get('currency');
$record = $query->result_array();
echo "<pre>";
print_r($record);
echo "</pre>";
}
}
Config Hook
$hook['pre_controller'] = array(
'class' => 'My_class',
'function' => 'my_function',
'filename' => 'My_class.php',
'filepath' => 'hooks',
);
Autoload the database better option
$autoload['libraries'] = array('database');
Codeigniter Hooks
Default ViewModel is not mandatory, I can return from controller just the array of data:
public function someAction()
{
//...
return array('result'=>$data);
}
But I can`t use this approach with Json. What should I do in dispatch event to wrap the results in JsonModel (for the appropriate accept header)?
Just create Base Controller for all your API controllers, and replace model in MvcEvent.
class JsonRestController extends AbstractRestfulController
{
public function onDispatch(MvcEvent $e)
{
$e->setViewModel(new JsonModel());
return parent::onDispatch($e);
}
}
You have to add a ViewJsonStrategy strategy to the view manager under your module.config.php:
'view_manager' => array(
'template_map' => array(
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy',
),
),
Then you can return a JsonModel in your action:
public function myAction()
{
$data = [1, 2, 3, 4];
return new JsonModel([
'data' => $data
]);
}
to get json data from controller you can echo json encoded data and exit. I use that for jquery ajax. i hope this is what you are looking for.
public function testAction()
{
$active = "some data";
echo json_encode(array('result' => $active));
exit();
}
then at jquery you can get this data like that
$.ajax({
type: 'GET',
url: '/index/time',
dataType: 'json',
error: function() {
$('#info').html('<p>Error on time calculation</p>');
},
success: function(data) {
data.result
}
});
Is really simple
Add as follows:
IndexController.php
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel; // Add this line
class IndexController extends AbstractActionController {
public function indexAction() {
// some index action
return new ViewModel();
}
public function apiAction() {
$person = array(
'first_name' => 'John',
'last_name' => 'Downe',
);
// Special attention to the next line
return new JsonModel(array(
'data' => $person,
));
}
}
api.phtml
<?php echo $this->json($this->data); ?>
Result:
{"first_name":"John","last_name":"Downe"}