Yii 2.0 How to extend Core Classes - yii

I want to extend the class yii\web\Response. So I created a new class Response in the folder components and I try to overwrite the send method.
namespace app\components;
use Yii;
class Response extends \yii\web\Response{
public function init(){
parent::init();
}
/**
* Sends the response to the client.
*/
public function send()
{ ...
Finally I tried to import my new Response-Class by importing it in the config.
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'import' => [
'class' => 'app\components\Response',
],
Why is it not going to work like this?

Try it like this:
'components' => [
'response' => [
'class' => 'app\components\Response',
],

Related

Problems with login and authentication in Laravel

I need help with my login and authentication of admin.
In the database I have a table called 'admins' with columns of 'name', 'surname', 'password' in my native language.
Every time I press the login button when I try to log in, I get an error:
"Undefined index: password"
where password is in English in folder:
C:\wamp\www\app\vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php
and I don't know why.
My custom controller AuthController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use Session;
use App\Models\Admin;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
public function index()
{
return view('auth.login');
}
public function customLogin(Request $request)
{
$request->validate([
'name' => 'required',
'surname' => 'required',
'passw' => 'required',
]);
$credentials = $request->only('name', 'surname', 'passw');
if (Auth::attempt($credentials)) {
return redirect()->intended('');
}
return redirect("login")->withSuccess('Wrong input data.');
}
public function dashboard()
{
if(Auth::check()){
return view('');
}
return redirect("login")->withSuccess('Wrong input data.');
}
public function signOut() {
Session::flush();
Auth::logout();
return Redirect('');
}
}
My route:
Auth::routes();
Route::post('/login', 'AuthController#customLogin');
I consulted with an acquaintance that specialises in web-programming and she said I should do a custom AuthController, which I did, but the problem is either still not fixed or this is a different error.
And from web sources I used:
https://www.positronx.io/laravel-custom-authentication-login-and-registration-tutorial/

Is there a way for getting a list of all created tests in my emberjs app?

I need a way to get all my ember tests' nameS, since I want to run them in a separate thread by using the --filter "TEST-NAME" flag.
Right now, I am using a hardcoded array.
It's not documented, but you can use the config object on the root QUnit object to get all modules in your test suite, then the tests within each module and their names. Note that any tests not in a module will still appear, but with an empty-named module. Here's a runnable jsfiddle with the example, then the code below.
QUnit.test('no-module', (assert) => { assert.equal(0,0) })
QUnit.module('foobar', () => {
QUnit.test('foo', (assert) => { assert.equal(1, 1) })
QUnit.test('bar', (assert) => { assert.equal(2, 2) })
})
QUnit.module('batbaz', () => {
QUnit.test('bat', (assert) => { assert.equal(3, 3) })
QUnit.test('baz', (assert) => { assert.equal(4, 4) })
})
const testNames = []
QUnit.config.modules.forEach((module) => {
testNames.push(...module.tests.map(test => test.name))
})
console.log(testNames)

Redirect to nested controller action

I recently moved my controllers to an Admin directory.
I changed the namespace: namespace App\Http\Controllers\Admin;
I have included the Controller class: use App\Http\Controllers\Controller;
In my controller, I have a redirect to the controller's index() action.
return redirect()->action('ServiceController#index');
Now I get the following error:
InvalidArgumentException Action
App\Http\Controllers\ServiceController#index not defined.
I can't figure out how to declare the new action redirect in the docs so I am posting my question here.
Routes
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function () {
Route::resource('projects', 'ProjectController');
Route::resource('services', 'ServiceController');
Route::resource('projectFiles', 'ProjectFileController');
Route::get('seed', 'SeedController#seedDatabase')->name('seed');
});
This is the part of the controller where I am talking about:
class ServiceController extends Controller
{
public function index()
{
return view('admin.services.index', [
'services' => Service::all()
]);
}
public function create()
{
return view('admin.services.create');
}
public function store(Request $request)
{
try {
Service::create([
'name' => $request->name,
'machine_name' => snake_case($request->name),
'description' => $request->description
]);
return redirect()->action('\App\Htpp\Controllers\Admin\ServiceController#index');
} catch (\Throwable $th) {
throw $th;
}
}
}
I think I found the answer, but anyone can correct me if I am wrong.
In RouteServiceProvider the namespace is set to App\Http\Controllers:
protected $namespace = 'App\Http\Controllers';
So I decided to add Admin\ before the name of the controller and now the redirect works:
return redirect()->action('Admin\ServiceController#index');

Mock #ngrx/store in angular 2

I am using store in my application like below and it works fine.
export class NavigationComponent {
navigationLinks$: Observable<Navigation[]>;
constructor(private store: Store<State>) {
this.navigationLinks$ = this.store.select('navigation')
.map((result: State) => result.navigationLinks);
}
Now, I am trying to create a unit test and want to mock this store. This is what i am doing:
1. Creating the Mock Store
Creating a mock store which will return mock data when this.store.select('') is called. The mockdata returns a property of array type called navigationLinks.
class StoreMock {
public dispatch(obj) {
console.log('dispatching from the mock store!')
}
public select(obj) {
console.log('selecting from the mock store!');
return Observable.of([
{ 'navigaitonLinks$': [{ 'name': 'Help', hasChild: false}] }
])
}
}
2. BeforeEach blocks
describe('NavigationComponent', () => {
let component: NavigationComponent;
let fixture: ComponentFixture<NavigationComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [NavigationComponent],
providers: [{provide: Store, useClass: StoreMock}],
imports: [
StoreModule.provideStore(reducers)
],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(NavigationComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
3. My test
I know this test will fail as per the expect statement but I am not able to populate the navigationLinks$ property with my mock data.
it(`should create the navigation with 'Help' link`, () => {
let navLinks: any[];
component.navigationLinks$.subscribe();
console.log(navLinks); // This should print my mockdata so i can assert it
expect(component.navigationLinks$).toEqual('Help');
});
The console.log prints undefined and is not able to read the data that MockStore select() is returning. Is there something extra I need to do?
I have the same issue, and I just return the object with Observable.of() function.
return Observable.of([
{ 'navigaitonLinks$': [{ 'name': 'Help', hasChild: false}] }
])
to
return Observable.of([{ 'name': 'Help', hasChild: false}, {}, {} ... ]);
This will populate your Observable object :
it(`should create the navigation with 'Help' link`, () => {
component.navigationLinks$.subscribe((links) => {
console.log(links); // This should print an array of Links
});
});

In Cakephp 3, Use another (not users but members) controller for login

I am using Cakephp 3 for building new application where user have to login for their account however I am hitting the login URL http://localserver.com/members/login and it redirects me to http://localserver.com/users/login
Look like the 'users' controller is set by default in Auth component. How can I override the default controller from 'users' to 'members'?
NOTE: The URLs are not LIVE as I am working on my local-server.
Yes, this is related to the userModel config key, which defaults to Users.
Try this script in your controller’s beforeFilter() or initialize() methods.
// Pass settings in
$this->Auth->config('authenticate', [
'Basic' => ['userModel' => 'Members'],
'Form' => ['userModel' => 'Members']
]);
Update:
In addition to userModel to be worked properly you must set the loginAction too.
// Pass settings in
$this->Auth->config('authenticate', [
'loginAction' => [
'controller' => 'Members',
'action' => 'login',
'plugin' => false, // or 'Members' if plugin
],
'Basic' => ['userModel' => 'Members'],
'Form' => ['userModel' => 'Members']
]);
Cookbook 3.x Doc