I'm building an authentication system and I use Composer to manage dependencies.
When I run my project it seems none of the classes can be found. I generated the files using cmd. I found similar threads here and here but it does not quite do it... Any idea?
The error
Fatal error: Class 'Bixxcom\User\User' not found in
C:\xampp\htdocs\authentication\app\start.php on line 34
start.php
<?php
use Slim\Slim;
use Slim\Views\Twig;
use Slim\Views\TwigExtension;
use Noodlehaus\Config;
use Bixxcom\User\User;
use Bixxcom\Helpers\Hash;
session_cache_limiter(false);
session_start();
ini_set('display_errors', 'On');
define('INC_ROOT', dirname(__DIR__));
require INC_ROOT . '/vendor/autoload.php';
$app = new Slim([
'mode' => file_get_contents(INC_ROOT . '/mode.php'),
'view' => new Twig(),
'templates.path' => INC_ROOT . '/app/views'
]);
$app->configureMode($app->config('mode'), function() use ($app){
$app->config = Config::load(INC_ROOT . "/app/config/{$app->mode}.php");
});
require 'database.php';
require 'routes.php';
$app->container->set('user', function(){
return new User;
} );
$app->container->singleton('hash', function() use ($app){
return new Hash ($app->config);
});
$view = $app->view();
$view->parserOptions = [
'debug' => $app->config->get('twig.debug')
];
$view->parserExtensions = [
new TwigExtension
];
composer.json
{
"autoload":{
"psr-4":{
"Bixxcom\\": "app/Bixxcom"
}
},
"require": {
"monolog/monolog": "1.0.*",
"slim/slim": "~2.0",
"slim/views": "0.1.*",
"twig/twig": "~1.0",
"phpmailer/phpmailer": "~5.2",
"hassankhan/config": "0.8.*",
"illuminate/database": "~5.0",
"ircmaxell/random-lib": "~1.1"
}
}
User.php
<?php
namespace Bixxcom\User;
use Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent
{
protected $table = 'users';
protected $fillable = [
'username',
'password',
'active',
'active_hash',
'recover_hash',
'remember_user',
'remember_token',
'first_name',
'last_name',
'age',
'email',
'phone',
'address',
'city',
'country'
];
}
Related
I'm doing a form on livewire as i always did it. But tonight it decides to mess with me.
I have my protected rules, and, in order to apply them, i have the "$this->validate" as you can see it on my code.
Problem is, with "$this->validate" nothing happens, no data in my table.
When i do it without "$this->validate"...it works..but of course, the rules are not applied.
Have you an idea?
`
<?php
namespace App\Http\Livewire;
use App\Models\Demande;
use Livewire\Component;
class CreateDemande extends Component
{
public $content, $annonce,
$phone, $mail, $user_id;
protected $rules = [
'content' => 'required',
'user_id' => 'required',
];
public function store()
{
$user_id = auth()->user()->id;
$this->validate();
Demande::create([
'content' => $this->content,
'user_id' => $user_id,
]);
}
public function render()
{
return view('livewire.create-demande');
}
}
`
Maybe a trait's missing ?
I tried to create a new component and doing it again, but doesn't worked too.
I tried to pass my rules in the validation :
`
$this->validate(
[
'content' => 'required',
'user_id' => 'required',
]);
Demande::create([
'content' => $this->content,
'user_id' => $user_id,
]);
`
Is there some error on my code i can't see here?
Edit : Sorry dont know why but the "hello" i wrote on the begining don't want to show him
Zend 3 translates form labels automatically.
If forms are created using array specification, how is it possible to scan translatable form element strings with Poedit?
How to add translator->translate() functionality to forms? I tried the following in module.php onBootstrap method but this does not work:
$sm = $e->getApplication()->getServiceManager();
$vhm = $sm->get('ViewHelperManager');
$translator = $sm->get('MvcTranslator');
$vhm->get('form')->setTranslator($translator);
I want to use it like $form->translator->translate(), in such a way it would be possible to scan code with Poedit to find translatable labeles, placeholders etc.
Here's a TranslatorFactory if you need
final class TranslatorFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
// get translator files' paths from config
$paths = $container->get('config')['settings']['localization-paths'] ?? [];
$translator = new Translator();
// add zend-i18n-resources files to translator
$translator->addTranslationFilePattern(
'phpArray',
Resources::getBasePath(),
Resources::getPatternForValidator()
);
// add your translation files to translator
foreach ($paths as $path) {
$translator->addTranslationFilePattern('phpArray', $path, '%s.php');
}
// todo: replace with user's preferred language
$translator->setLocale('tr');
return $translator;
}
}
And add your factory to service manager
'service_manager' => [
'factories' => [
\Zend\I18n\Translator\TranslatorInterface::class => \MyModule\Factory\TranslatorFactory::class,
],
],
Not sure if you're still looking for a solution, so I'll add mine.
I use the TranslatorAwareTrait in my AbstractForm class.
use Zend\I18n\Translator\TranslatorAwareTrait;
abstract class AbstractForm extends \Zend\Form\Form implements
{
use TranslatorAwareTrait;
// Form stuff
}
Then, in the *FormFactory do the following:
use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\Factory\FactoryInterface;
class SomeFormFactory implements FactoryInterface
{
/**
* #param ContainerInterface $container
* #param string $requestedName
* #param array|null $options
* #return mixed|object|AbstractForm
* #throws \Psr\Container\ContainerExceptionInterface
* #throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
// Obviously you'll have more/other requirements. Fulfil them here.
$form = new SomeForm();
$form->setTranslator(
$container->get('translator')
);
return $form;
}
}
Usage example:
use Zend\I18n\Translator\TranslatorAwareTrait;
abstract class AbstractForm extends \Zend\Form\Form implements
{
use TranslatorAwareTrait;
public function init()
{
if (!$this->has('submit')) {
$this->addSubmitButton();
}
}
public function addSubmitButton($value = 'Save', array $classes = null)
{
$this->add([
'name' => 'submit',
'type' => Submit::class,
'attributes' => [
'value' =>
// Translate $value before passing to this function
$value === 'Save' ? $this->getTranslator()->translate('Save') : $value,
'class' => (!is_null($classes) ? join (' ', $classes) : 'btn btn-primary'),
],
]);
}
}
On the other hand, you could...
Translate strings before passing them if you're translating with Poedit.
If your modules contain the following config (in each module!):
'translator' => [
'translation_file_patterns' => [
[
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
],
],
],
You can see here that translation is done using gettext. This is a PHP module that searches for the following code strings and translates its contents: _('translatable string').
The translation files to look for end with the .mo extension and can be found in __DIR__ . '/../language'.
Thus, if you make sure to have the PHP gettext module enabled to use this.
To use this with just normal strings, even in config for a Fieldset or a form, you could have the following:
$this->add([
'name' => 'street',
'required' => true,
'type' => Text::class,
'options' => [
'label' => _('Street'), // <<-- translated using gettext
],
]);
I've got a problem ith redirect after successful login.
App.php file (part of it):
<?php
use Silex\Application;
/** ... */
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
$app = new Application();
/** ... */
$app['security.encoder.digest'] = $app->share(function ($app) {
return new PlaintextPasswordEncoder();
});
$app['security.firewalls'] = array(
'secured' => array(
'pattern' => '^/admin/',
'form' => array(
'login_path' => '/login',
'check_path' => '/admin/login_check',
'default_target_path' => '/admin/news',
'always_use_default_target_path' => true,
),
'logout' => array(
'logout_path' => '/admin/logout',
),
'users' => array(
'admin' => array('ROLE_ADMIN', 'test'),
),
),
);
My controllers.php file:
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app->get('/login', function(Request $request) use ($app) {
return $app['twig']->render('login.html.twig', array(
'error' => $app['security.last_error']($request),
'last_username' => $app['session']->get('_security.last_username'),
));
});
$app->get('/admin/news', function(Request $request) use ($app) {
if ($app['security.authorization_checker']->isGranted('ROLE_ADMIN')) {
echo 'admin';
}
});
I am logged in (after passing correct login data in form), I have access to /admin/news route, but I have to go to that address manually. I'd like to be redirected there automatically after successful login. Now I'm kept in /login page. When I change default_target_path to 'http://google.com', it works properly.
I use homestead (vagrant) with nginx. Silex 1.3.0.
I'll appreciate any help.
The solution (or rather workaround) which worked was:
change 'default_target_path' to '/admin';
add new route:
$app->get('/admin', function() use ($app) {
return $app['twig']->render('admin/loggedIn.html.twig');
});
in admin/loggedIn.html.twig template I've added js redirect to admin/news
I don't have any idea why wasn't it working. If anyone knows - please leave a comment.
Hello everyone please someone help me how to create a global variable in zend framework 2 to be use in table prefix that are accessible in controller and model.
Thanks and regards to all.
In your config/database.local.php you can define which you want globally
<?
return array(
'service_manager' => array(
'factories' => array(
//'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
$adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
$adapter = $adapterFactory->createService($serviceManager);
\Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
return $adapter;
}
),
),
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=testdb;host=localhost',
'username' => 'root',
'password' => '',
),
'msg' => array(
'add' => 'Data Inserted Successfully',
'edit' => 'Data Updated Successfully',
'delete' => 'Data Deleted Successfully',
),
);
?>
Controller File:
DemoController.php
<?php
namespace Demo\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class DemoController extends AbstractActionController
{
public function indexAction($cms_page_name='whyus')
{
/*Call config file to fetch current cms page id-- fetch config file from database.local.php*/
$config = $this->getServiceLocator()->get('Config');
$all_msg = $config['msg'];
}
}
?>
i developing a site with zend framework.
i use autoload for load a class.
it work on controller, on model but not work in bootstrap file.
why?
bootstrap.php
protected function _initAutoload ()
{
// Add autoloader empty namespace
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array('basePath' => APPLICATION_PATH, 'namespace' => '',
'resourceTypes' => array(
'form' => array('path' => 'forms/', 'namespace' => 'Form_'),
'model' => array('path' => 'models/', 'namespace' => 'Model_'),
'plugin' => array('path' => 'plugin/', 'namespace' => 'Plugin_'))));
// viene restituto l'oggetto per essere utilizzato e memorizzato nel bootstrap
return $autoLoader;
}
/**
* inizializza l'autenticazione
*/
protected function _initAuth ()
{
$this->bootstrap("db");
$this->bootstrap("Autoload");
$db = $this->getPluginResource('db')->getDbAdapter();
$adp = new Zend_Auth_Adapter_DbTable($db);
$adp->setTableName(USERS_TABLE)
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('sha1(?)');
$storage = new Model_Sessions(false, $db);//line 81
$auth = Zend_Auth::getInstance();
$auth->setStorage($storage);
//$this->bootstrap('log');$log=$this->getResource('log');
if ($auth->hasIdentity()) {
$identity = $auth->getIdentity();
$user = $identity->user_id;
} else
$user = 1;
$user = new Model_user($user);
}
output error
Fatal error: Class 'Model_Sessions' not found in /application/Bootstrap.php on line 81
in Session.php
<?php
/**
* #method get($k,$dv=FALSE)
*/
class Model_Sessions implements Zend_Auth_Storage_Interface
{
Your resource autoloader looks good.
I suspect you want Model_Sessions, not Model_sessions (not lower/upper case on "sessions").
Make sure the class Model_Sessions is stored in file application/models/Sessions.php
As a side note, you have your resource autoloader looking for plugins with namespace prefix plugins_. Again, here, I suspect you want uppercase Plugins_.