BadMethodCallException in Macroable.php line 74 : Method controller does not exist - laravel-5

I'm getting little bit issue when I follow Route Controller.
Web.php Code:
{"
Route::controller('/admin','adminController');
"}
adminController.php Code:
{"
<?php
namespace App\Http\Controllers;
class adminController extends Controller{
public function getDashboard(){
echo " Get Dashborad Method ";
}}
"}
When I hit http://localhost:8000/admin/dashboard
an error:
Display ("BadMethodCallException in Macroable.php line 74 : Method controller does not exist.")
Here is SnapShot:
Please take a look and let me know what is wrong with code.

Please also make sure you don't use any namespace inside your routes file.
E.g. If by mistake your IDE add's
use Illuminate\Routing\Route;
It could result in the same error mentioned above. Your route file (web.php or api.php) should not use the Illuminate\Routing\Route class.
EDIT:
Tested on Laravel 5.5

Faced the same problem recently. Laravel 5.3 does not support Route::controller() method. You need to change it to Route::get().
Please check how to use it here https://laravel.com/docs/5.3/routing#basic-routing.

I got this error while running artisan command. Finally, I solve by removing the,
use Illuminate\Routing\Route
in web.php file.

i have just solved this problem i was using
$table->int('TeachingGroup_id');
where i should use the full integer Not int only by solving this now migration done S

Related

Laravel env() value null

I'am trying to retrieve EXAMPLE_URL=www.google.com from .env in controller and everytime get null. Where is the problem because the same code works on another application. This function doesn't work after php artisan cache:clear.
Controller code
$hostname = env("EXAMPLE_URL");
dump($hostname);
You should not use env() outside of the config files.
Read: https://laravel.com/docs/8.x/configuration
You should add the env variable to a config file and use config('example.url');.
The example.php would look like:
return [
'url' => env('EXAMPLE_URL', 'https://example.com'),
];
Always use the app file as the middleman
see : https://laravel.com/docs/9.x/configuration

The use statement with non-compound name 'Session' has no effect

$ php artisan serve
ErrorException
The use statement with non-compound name 'Session' has no effect
at routes/web.php:6
3▕ use Illuminate\Support\Facades\Route;
4▕ use Illuminate\Http\Request;
5▕ use Illuminate\Foundation\Application;
➜ 6▕ use Session;
7▕
The error says that doing use Session in the root namespace does not actually do anything because Session is already in the root namespace.
In reality Session does not exist in the root namespace but is rather an alias defined in config/app.php. Bottom line is when you are in the root namespace already you don't need to do use Session.
While you don't need to do this, what I suggest you do is:
use Illuminate\Support\Facades\Session
Alternatively you can use the session helper function:
session()
and you can also retrieve the session singleton using the application container:
app()->make('session');
This is just because personally I don't like global aliases. Your opinion may differ.

How to use ENV variables in Artisan Commands in Laravel 5.8?

I am creating my own artisan command and I want to use ENV variables, but when I use $_ENV['VariableName'] I get and error.
local.ERROR: Undefined index: VariableName
The same code works perfectly in a controller and error as this one is not being generated.
I am creating my commands with php artisan make:command CommandName
How can I start using ENV variables there? Thank you! I want to use the variables in a private function which is inside:
class CommandName extends Command but outside the public function handle()
Since the .env file is not pushed to the repository, the best approach is to use config files instead. So in the config directory, create your custom file for example: custom.php with the following content:
<?php
return [
'variable' => env('VARIABLE_NAME', 'DEFAULT_VALUE')
];
and in your .env you should put:
VARIABLE_NAME=something
Then to use it you use config('custom.variable');
You can use the Laravel Helper to access environment variables with something like this:
env('VariableName')
you can also specify a default value if the environment variable is not set
env('VariableName', 'myName')
Laravel Docs 5.8 Helpers

Artisan says class name not defined, even though it is?

I have created the following Artisan command in Laravel:
class draft_cron extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'draft_cron';
//etc
And in start/artisan.php I added:
Artisan::add(new draft_cron);
However, when I try to run the command, I get an error saying Class 'draft_cron' not found and pointing to the above line from start/artisan.php.
Any ideas what the problem might be?
2 things: 1 is what user RDelorier said. DraftCron instead of draft_cron. The other thing is, did you do composer dump-autoload?
Most likely culprit is PSR cant figure out where the class is, any particular reason you used snake_case over StudlyCase?

CodeIgniter Cron Job through Cpanel

Ok I looked at every other thread and have done exactly what they've done and what it says in the manual and I can NOT figure this out for the life of me.
The results of the cron job are being emailed to one of my emails. ALL it is doing is printing out the html markup of the layout... And printing the base page content... It's like it's not registering anything.
php /home/jdstable/public_html/dev/index.php cron decrease_pets_stats
That's my command line.. I tried replacing php with the user/local/bin/php thing as well and it didn't work. The thing is is that I have other cron jobs running off procedural PHP code that work FINE with php path/to/cron.php... But it won't work with CI..
My controller is Cron and my method is decrease_pets_stats..
//decrease pets stats
public function decrease_pets_stats() {
$this->load->model('Cron_model', 'cron');
$this->cron->decrease_pets_stats();
echo 'Decreased pet stats';
}
And here is the logic of the method:
//decrease pets stats
//runs every hour
public function decrease_pets_stats() {
$this->db->set('hunger', 'hunger - 5');
$this->db->set('happiness', 'happiness - 5');
$this->db->set('loyalty', 'loyalty - 5');
$this->db->update('user_creature');
}
Does anyone have any idea why it's just printing the layout markup? My constructor looks like this:
public function __construct() {
parent::__construct();
if( ! $this->input->is_cli_request()) show_error('Direct access is not allowed');
$this->load->model('Cron_model', 'cron');
}
And my parent constructor holds quite a bit of stuff (loading helpers and libraries along with getting the user information to appear on each page if they are logged in.
Does it matter if this is at the top of the controller before even opening the controller Cron class?
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
The default PHP install you are using was probably compiled as CGI-FCGI, not for CLI. It depends on your host and/or server, but you'll need to search for your PHP install for the command line interface, and then use that in your cron job. I had the exact same problem on Hostmonster, and my cron command ended up being:
/ramdisk/bin/php5-cli ~/public_html/sitefolder/index.php controller method
For me, the PHP I needed was in /ramdisk/bin/php5-cli.
For CodeIgniter 2.2.0
You can try this two method:
php-cli /home/username/public_html/index.php controller method
wget http://www.example.com/controller/method
or at your case
php-cli /home/username/public_html/index.php Cron decrease_pets_stats
wget http://www.example.com/Cron/decrease_pets_stats
It works fine with me..
Cheers!
Had the same issue and after trying whatever I could thing the obvious worked...
/usr/local/bin/php /absolute/path/to/index.php cron
/usr/local/bin/php /home/jdstable/public_html/dev/index.php cron decrease_pets_stats
This fixed it.
Here is solution first you need to find path from phpinfo document_root file name
php5 /home/abc/public_html/index.php folder_name controller function

Resources