I am using laravel to have access to my database. I have used the command php artisan make:controller CategoriesController --resource in the terminal to create a class where I can access different methods in one route.
The routing code for the class is: Route::apiResource("categories", CategoriesController::class);. With /categories I can get to the index() method (via get), where I can show my table values. But I do not know how I can use other methods. For example I have created test() with a simple return ["Result"=>"Working"].
I have tried /categories/test /categoriestest /categories%test but I can not show the result from test().
Simple routing works fine when I use specific routes for every method, but I want to make a more clear code so I would like to use the --resource to only have one route.
Storage::putFile('uploads', new File('C:\sandbox\sample.txt')); is storing the file sample.txt with hashed name into the uploads directory everytime I save this code in VSCode without me executing this php code in browser using WAMP or terminal. I am not running any unit tests and also did not even open this page in browser. How is this happening?
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\File;
class WelcomeController extends Controller
{
public function index()
{
Storage::putFile('uploads', new File('C:\sandbox\sample.txt'));
}
}
I expect this code to run when the controller method containing this code is executed but this is storing the file on just saving this code.
I have a controller method that I want to run every minute. I read the documentation for Task Scheduling but it seems to document Commands only.
Is there any way that I can use it to call a route/controller method/script every minute ?
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Acc;
class ScriptController extends Controller
{
public function updateAcc()
{
$c = new Scripts\AccountA();
$xoo = $c->getInfo();
$z = json_decode($xoo,true);
$data= new Acc();
$data->price = $z["result"];
$data->save();
}
}
I need to use the DB facades and external classes etc...
Yes, just like the documentation states, you can make a call to any function. So you can do the following:
$schedule->call(function () {
$controller = new \App\Http\Controllers\ScriptController();
$controller->UpdateAcc();
})->everyMinute();
However, it is very bad practice to call a controller out of its web context, you should create a facade or a job that executes the code you want.
Yes, you can write a a shell script that will make a CurlRequest to your controller, and add the shell script to a cron job. or you can use laravel commands and call the controller by a request.
why not using the code inside the controller in a command?
The error I'm getting is that the controller doesn't exist even though I know it does, here's the code.
Route.php
Route::get('mdpay/template', array("uses" => "templateController#index"));
templateController.blade.php
class templateController extends BaseController {
public function index()
{
echo "made it";
}
}
Why might I be getting this error: Class TemplateController does not exist
================= UPDATE: ==================
Ok, so I've created the correct route, renamed my file, and corrected the class name and I'm still coming up with that error.
File Names:
templateController.php
// File Name: TemplateController.php
class TemplateController extends BaseController {
public function index()
{
// app/views/myView.blade.php
echo "hello";
}
}
My route is:
Route::get('mdpay/template', array("uses" => "TemplateController#index"));
Still receiving Controller Doesn't Exist error. All my other controllers (3 others) are working except this one.
If you are using the standard composer classmap autoloader you need to composer dumpautoload everytime you create a new file.
So to create a new controller with the standard composer setup given by Laravel:
Create a new file in app/controllers named TemplateController.php
Open up terminal and run composer dumpautoload
As previous users have said, only view files should end with .blade.php.
If you're using Laravel 8, add this line to your RouteServiceProvider.php (you can search it by using CTRL + P):
protected $namespace = 'App\Http\Controllers';
This solved the issue for me.
It should be:
// File Name: TemplateController.php
class TemplateController extends BaseController {
public function index()
{
// return "made it"; // or
// app/views/myView.blade.php
return View::make('myView');
}
}
Route for that:
Route::get('mdpay/template', array("uses" => "TemplateController#index"));
Use blade in a Blade view, i.e: myView.blade.php basically stored in app/views/ folder. Read more about blate template on Laravel website.
Controllers live in the app/controllers directory and should remain there unless you have your own namespaced structure.
The reason you're getting a Class TemplateController does not exist is because it doesn't, firstly, your class is called templateController and secondly, it exists as templateController.blade.php which wouldn't be loaded in this way.
Blade files are for views, and only views within app/views or a custom views directory should end with .blade.php.
Create the file app/controllers/TemplateController.php and add the following code to it.
class TemplateController extends BaseController {
public function index()
{
echo "made it";
}
}
Now on the command line, run the command composer dumpautoload and change you route declaration to:
Route::get('mdpay/template', array('uses' => 'TemplateController#index"));
Now it should all work.
In case you're using Laravel 9 and the error is like Illuminate\Contracts\Container\BindingResolutionException and Target class <controller name> does not exist. when trying php artisan route:list on terminal.
This is the setup that I do:
Add protected $namespace = 'App\\Http\\Controllers'; to RouteServiceProvider.php
Add 'namespace App\Http\Controllers;' to the controller file.
Do php artisan optimize on terminal
(Just to make sure the route already there) Do php artisan route:list again on terminal, and the controller route should be displayed.
I'm trying to use yiic to run an action that's inside a controller (protected/controllers/site.php)
class SiteController extends Controller {
public function actionHello() {
echo 'hello!';
}
}
If I try to run (inside protected/ folder) ./yiic site hello
It says it only has the commands message, migrate, shell and webapp.
How do I call this action within the Command Line ?
Short answer. You can't :-) You need to create Yii an override of CConsoleCommand (more info on the Yii guide here).
Once you've done that, then you create an action (or shift your code right over to that action).