Laravel 4 class not found error - laravel

I'm getting an error that a class (a controller) is not being found. Thing is, it is found in my local development environment. I created the controller with php artisan controller:make CssController --path=app/controllers/home.
I renamed the class (not the file) Home_CssController. I added the route:
Route::get('home/css-php', 'Home_CssController#index');
to my routes file. Everything works fine in my local environment. I did forget to run composer dump-autoload but there were no issues with viewing the controller/view in my local environment. I've uploaded everything to the live server, but I'm getting the error:
Class Home_CssController does not exist
I've uploaded the controller, the routes file and the view multiple times. But I still get the error. Is it because the controller wasn't registered with composer? I've since registered it, but am not sure what I need to upload to the server. I've uploaded both the config and bootstrap folders. What do I need to do to get the controller/view to be found? This is Laravel 4.

If the controller isn't in the app/controllers folder, then it will need to be namespaced (unless you want to continue using your autoload.php trick).
Namespace Home_CssController to Home.
<?php namespace Home;
class CssController extends \BaseController
Then, you can use it in your routes:
Route::get('home/css-php', 'Home\CssController#index');

Related

OctoberCMS and BotMan

I want to integrate a chatbot (BotMan version 2.0) into an existing OctoberCMS project , here is what I did till now :
1- I added BotMan to the projct using the following command :
composer require botman/botman
2- I created a routes.php file in the same directory as the plugin.php file
<?php
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Drivers\DriverManager;
//Route::match(['get', 'post'], '/botman', 'BotManController#handle');
//Route::get('/botman/tinker', 'October\Demo\BotManController#tinker');
// Create an instance
$botman = BotManFactory::create($config);
// Give the bot something to listen for.
$botman->hears('hello', function (BotMan $bot) {
$bot->reply('Hello yourself.');
});
// Start listening
$botman->listen();
My questions are :
1- Where I have to add the BotManController.php file
2- I tried to add BotManController.php in the same directory as the routes.php file but I get the following error :
Class 'App\Http\Controllers\Controller' not found
( and thats because its an OctoberCMS project not Laravel project ... I dont have the App directory )
Can anyone please provide me a solution to that or another way to integrate Botman into OctoberCMS !
First off, read https://luketowers.ca/blog/how-to-use-laravel-packages-in-october-cms-plugins/ as well as https://octobercms.com/docs/plugin/composer.
Secondly, you can put the BotManController file anywhere in your plugin you want as long as you understand PHP namespaces and how to properly reference it where you want to. I would probably recommend putting it in a /classes/ directory under your plugin folder, and then changing the App\Http\Controllers\Controller reference to instead reference the base Illuminate\Routing\Controller class. You could also put it in a /controllers/ directory, but in OctoberCMS that is typically reserved for your backend controllers so I wouldn't recommend mixing the two.

Laravel Spark controller routes

I upgraded my Laravel Spark to version 6 and now I get the error ...
Class App\Http\Controllers\TeamController does not exist
This was not a problem previously, so I took a look at the routes files in /vendor/laravel/spark-aurelius/Http and compared them with previous versions. Up till version 5, the route was ...
$router->get('/'.$pluralTeamString.'', 'TeamController#all');
In version 6, the route is ...
$router->get('/settings/'.Spark::teamsPrefix(), 'TeamController#all');
I tried altering my routes file from this ...
$router->get('/teams', 'TeamController#all');
To several configurations of \settings\TeamController but couldn't get one to work. Am I missing something here?
Here are the pertinent parts of the Team Controller code:
namespace Laravel\Spark\Http\Controllers;
use Laravel\Spark\Spark;
use Illuminate\Http\Request;
use Laravel\Spark\Contracts\Repositories\TeamRepository;
class TeamController extends Controller
{
...
#joshua-foxworth Very likely you are trying to create the route on web.php under the routes folder. We know web.php by default checks for controllers under app -> Http -> Controllers folder. You are looking for this route file here from root dir spark/src/Http/routes.php Hope this solves your problem.

How to generate a core controller in Laravel?

I have deleted the main controller Laravel by path: App\Http\Controllers\Controller.php;
All new controllers extent this controller:
class MainController extends Controller
{
}
How to generate a core controller in Laravel again?
I think you can't regenerate core Controller class via command neither artisan nor composer . You have to copy this from github or another project.
I don't think either you are able to regenerate it. But you can get a copy of the content here

Dynamic redirect in package route

I have defined a route action with some business logic, inside an internally developed package. Depending on the result in this action, the app want to redirect the user to some dynamic route (Redirect::route('admin.index', [$app->id]) e.g).
How would I do this?
Any solution I come up with doesn't work because of the way Laravel handles routes.
Right now I have copied the route to the app routes.php, and extracted the business logic to a method inside the package. But this is not optimal, as I'd like to also keep the route inside the package.
Laravel has some documentation on Package Configuration that should work for you.
In your package's src/config/config.php:
<?php
return array(
'route_admin_index' => 'admin.index',
);
Change your package's code to:
Redirect::route(Config::get('your_package_name::route_admin_index'), [$app->id]);
Now when installed on different environments, you can do:
php artisan config:publish your_vendor_name/your_package_name
Which will publish your package's configuration file to:
app/config/packages/your_vendor_name/your_package_name
Where then you can change the route_admin_index at will.
If php artisan config:publish was not called. Your route will default back to what you have in your package's config file.

Laravel Controller not working

I'm very new to the Laravel framework and am trying to load a simple controller in my browser to slowly get the hang of things.
I have a file that's titled users.php inside of the the laravel/app/controllers/ folder and it looks like this:
class UsersController extends BaseController
{
public $restful = true;
public function action_index()
{
echo 'hi';
}
}
In the routes.php file, I have
Route::get('users', 'UsersController#index');
But, when I go to
http://localhost:8888/laravel/public/users
I'm greeted with a message that says "ReflectionException
Class UsersController does not exist"
I'm not sure if this is because I didn't install the mcrypt extension of PHP. But, when I checked the php.ini file on MAMP, it said that it was enabled. Upon entering
which PHP
in my terminal, it said /usr/bin/php. So, it might not be using the correct version of PHP.
I'm not entirely sure if this is a routes problem or if it's stemming from an absence of a vital PHP extension.
Thanks a bunch!
You need to use the Route::controller method to reference your Controller:
Route::controller('test', 'TestController');
...and rename your file (as Cryode mentions ) to be TestController.php.
Note - if you want to use the filename as test.php, then you will need to use composer to update the autoload settings.
Finally, the format of names for Controller methods changed in Laravel 4, try renaming the method
public function action_index() {}
to be
public function getIndex() {}
the get represents a HTTP GET request... the same applies for post (HTTP POST) and any (GET or POST.. )
I'm not familiar with that part of Laravel's source, so I'm not entirely certain that this is the issue, but your controller file name should match the controller class name, including capitalization.
So users.php should be UsersController.php. Now, when I do this myself on purpose, I get a "No such file or directory" error on an include() call, so that's why I'm not certain that's the sole cause of your problem. But it may be a start.

Resources