Laravel 4.2 to 5 Class 'App\Http\Controllers\Controller' not found - exception

I upgraded my project from Laravel 4.2 to 5.0 but I get this error when I finish the process:
Class 'App\Http\Controllers\Controller' not found' in .../app/Http/Controllers/Auth/AuthController.php:8
But the mentioned controller is there, in app/Http/Controllers/Controller.php.
Also it is defined in composer.json, autoload, classmap:
"autoload": {
"classmap": [
"database",
"app/Http/Controllers",
"app/Libraries"
],
"psr-4": {
"App\\": "app/"
}
},
Apparently this is a namespace problem, but I don't know hot to solve it

In 99% of the cases the main cause of classes being not found when you migrate a Laravel 4 project to Laravel 5 is the lack of Namespaces
It is important to add namespaces to all your classes, controllers, old filters as middleware, etc.

Just add the file/directory to your composer like that.
"autoload": {
"classmap": [
"app/Http/Controllers/Controller.php"
],
There are a lot of other ways too.
Or use psr-0,psr-4 to autoload the directory/file. Or you load this file in global.php.

I had the same problem. Following the upgrade guide (http://laravel.com/docs/5.0/upgrade#upgrade-5.0) the migration went fine but then when I started playing with Auth, I got the same error.
The reasons were that I followed the upgrade guide. When it says "Since we are not going to migrate to full namespacing in this guide", in fact you should use namespaces in your controllers with at their top
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
And then unwind what you did in the Controllers section of the upgrade guide. Then after running composer dump-autoload, it will work.

Related

Composer autoload file before loading dependencies

One of the vendor packages I'm requiring exposes a function by autoloading it:
{
...
"autoload": {
...
"files": [
"functions.php"
]
}
}
Unfortunately it lacks multilingual support.
As I really like the function's fluent definition and I already have a drop-in multilingual alternative., I would like to use my drop-in replacement instead while still using the same function name. Luckily the package has implemented a function_exists() check, so I just need to declare my function before the package is autoloaded.
I know I could just reference a require before requiring Composer's autoloader, but are there any composer.json alternatives to accomplish this? Is there any way I can tell Composer to load a specific file first, before anything else?
More specifically I'm looking to override Laravel's unlocalized now() function to a localizable version. Since this question is also applicable to other applications and/or dependencies I kept my question quite generic.

babel-jest with ES6 modules

I'm happily using node 8.6 with the experimental ES6 modules option (--experimental-modules) turned on. This allows me to perfectly write plain ES2015 code for node without the need of babel.
The problem is when I try to create some tests with jest, it fails complaining about a syntax error: "Unexpected token import".
The .babelrc configuration is the following:
{
"env": {
"test": {
"presets": [
["env", {
"targets": {
"node": "8.6"
}
}]
]
}
}
}
My jest.config.js is as follows:
module.exports = {
testMatch: ['/tests/**/*.js', '**/?(*.)test.js'],
}
The error thrown:
/app/tests/integration/controller/data-provider/Credentials/CredentialsList.action.test.js:2
import { Credentials, AdWordsCredentials } from '../../../../../imports/models/data-provider/Credentials.mjs';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
at Generator.next (<anonymous>)
at Promise (<anonymous>)
Relevant packages:
babel-core#^6.26.0
jest#^21.2.1
babel-jest#^21.2.0
babel-preset-env#^1.6.0
Any help will be appreciated.
Thanks :)
UPDATE: I've tried calling jest without babel, with the following command, without any change: node --experimental-modules node_modules/.bin/jest
Jest has a custom implementation of require to help with mocking. Unfortunately, this makes jest incompatible with node --experimental-modules. Babel is probably the best way to use ES6 modules with jest. See https://github.com/facebook/jest/issues/4842
I was not used jest, and I am not sure if this will solve, but I hope this can help you.
Node still doesn't support all syntax. If you really are looking a faster way to start develop, using source code with all features of Ecmascript2017, you need a module like #kawix/core https://www.npmjs.com/package/#kawix/core
How the README.md says, allows you to use all features including "imports" and "async/await" and also supports typescript, and other good features all without a LOT OF DEPENDENCIES. You can use directly with cli:
> npm install -g #kawix/core
> kwcore /path/to/fullsyntaxtsupport.js
Or if you want inclute programatically, create a file example main.js to import the fully syntax file
var kawix= require("#kawix/core")
kawix.KModule.injectImport()
kawix.KModule.import("/path/to/fullsyntaxtsupport.js").catch(function(e){
console.error("Some error: ",e)
})

How to override ASP.NET Core configuration array settings using environment variables

TL;DR
In an ASP.NET Core app, I have an appsettings.json config file which uses a JSON array to configure a collection of settings.
How do I override a setting of one of the array objects using environment variables?
Background
I'm using Serilog in an ASP.NET Core application and using the Serilog.Settings.Configuration, to allow it to be configured using appsettings.json.
The configuration is like this:
{
"Serilog": {
"Using": ["Serilog.Sinks.Literate"],
"MinimumLevel": "Debug",
"WriteTo": [
{ "Name": "File", "Args": { "path": "%TEMP%\\Logs\\serilog-configuration-sample.txt" } }
],
"Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
"Properties": {
"Application": "Sample"
}
}
}
When deployed, I want to override some of the settings, e.g. the MinimumLevel, and the path to the log file. My preferred option is to do this via environment variables as I'm deploying to an Azure App Service, so I'll use the App settings through the Azure management portal (these are realised as environment variables).
I can easily set the MinimumLevel by adding an environment variable with the name: Serilog:MinimumLevel and the application name Serilog:Properties:Application.
What is the format for specifying a setting with an array?
After looking at the configuration in the debugger I found the answer.
Serilog__WriteTo__0__Args__path (All platforms)
Serilog:WriteTo:0:Args:path (Windows)
Serilog--WriteTo--0--Args--path (sourced From Azure Key Vault)
Note: The Configuration in ASP.NET Core documentation now covers this.
So I need to use the array index (zero-based) as if it were a name.
Here is the screenshot of the debugger, also (thanks to Victor Hurdugaci in the comments), the unit tests are a good place to look for examples.
I know this is an old thread, but the most relevant one I could find for my question:
My appsettings file has a setting like this:
{
"Settings": {
"UserList": [ "devuser1", "devuser2" ]
}
}
I want to put this into a k8s secret(because the list between environments changes), so I set the key-value pair as follows:
Settings__UserList: '[ "devuser1", "devuser2" ]'
But that doesn't seem to be working. I just realized I have yet to try setting it without the brackets:
Settings__UserList: "devuser1", "devuser2"
If that doesn't work, am I going to have to setup my secret like this to really get it to work?
Settings__UserList__0: "devuser1"
Settings__UserList__1: "devuser2"
I didn't see anything in the documentation that covered this type of array value.

UseRowNumberForPaging is not a valid

I'm using "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final" with SQL 2008 and according to some results found on Google, I just have to add the option to .UseRowNmberForPaging() when creating a new DBcontext. This was the solution for rc1-final but it doesn't seem to work for rc2-final.
When I add the option when configuring my service, it's not recognized.
Trying to paginate records on SQL Server 2008 with EF Core so this seems to be the recommended solution.
Here is the line I'm using to Configure the service:
services.AddDbContext<Data.Models.AC_MCLContext>(options =>
options.UseSqlServer(connection).UseRowNumberForPaging());
Does anyone know how to use the row number for paging in EntityFramework Core rc2?
A solution was given to me on another forum so I thought I'd share the answer in case anyone else ran into this issue.
The API now uses a nested closure pattern so the options should be configured as a nested structure like the example below.
services.AddDbContext<Data.Models.AC_MCLContext>(options =>
options.UseSqlServer(connection,
opt => opt.UseRowNumberForPaging()));
This can also be done from the context itself.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(yourConnectionStringValue, opt=>opt.UseRowNumberForPaging());
}
2022 update: using .NET 6 + EF Core 6 + SQL Server 2008, you can install the package bellow (wich has a GitHub repository, so the code can be checked for safety).
https://www.nuget.org/packages/EntityFrameworkCore.UseRowNumberForPaging/
After installing the package, in Program.cs add:
using EntityFrameworkCore.UseRowNumberForPaging;
Then replace your DbContext service configuration:
builder.Services.AddDbContext<YourDbContext>(options => options.UseSqlServer(connectionString));
For:
builder.Services.AddDbContext<YourDbContext>(options => options.UseSqlServer(connectionString, o => o.UseRowNumberForPaging()));

Cakephp 3 App::paths() gone, what do I use now?

In CakePHP 2.X, you could have files in multiple places and cakephp would iterate over the available places until it found the file. An example in CakePHP 2.X would look something like this.
App::build(array(
'Controller' => array(
ROOT.DS.'Customize'.DS.'Path2'.DS.'Controller'.DS,
ROOT.DS.APP_DIR.DS.'Controller'.DS
)
));
How does CakePHP 3 accomplish this? For example if I install a plugin with composer and I want to overwrite a controller file only within that plugin from my app, how would I go about doing that, and where are the paths that CakePHP 3 iterates through defined?
Found the answer in the CakePHP 3.0 book : http://book.cakephp.org/3.0/en/development/configuration.html#additional-class-paths
Using your composer.json file. It looks in the first path first, and the second path second.
"autoload": {
"psr-4": {
"App\\": ["./path1/path1/src", "src"]
}
},