I need use a PHP function to convert numbers to letters. I create a file named: converter.php in the folder “Controller”. I need call the function named “convertir_numero_letras($number)” insert in converter.php.
I call the function “convertir_numero_letras($number)” from the following controller:
public function convertirAction()
{
$number=1234;
$this->convertir_numero_letras($number);
return $this->render('contratos/mostrar_cifra.html.twig', array('numero_convertido' => $numero_convertido));
}
But this code not work. I obtain the following
error message:
Attempted to call an undefined method named "convertir_numero_letras" of class "BackendBundle\Controller\ContratosController".
As I can fix this?
you can create a folder inside bundle like yourBundel/Helper
and create you custom class in folder like ConverHelper.php
like this :
class ConvertHelper
{
public static function numero_letras($param)
{
return 'converted value';
}
}
and then you can call in any controller and any bundle like this:
yourBundle\helper\ConverterHelper::numero_letras('someting');
don't forget to add namespace of ConverterHelper to your contoller file
example:
// file: GRF/BlogBundle/Helper/ConverterHelper.php
<?php
namespace GRF\BlogBundle\Helper;
class ConverterHelper
{
public static function toNum($param)
{
return $param;
}
}
and usage in controller:
//file controller
<?php
namespace GRF\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class PostController extends Controller
{
public function indexAction()
{
return new Response(\GRF\BlogBundle\Helper\ConverterHelper::toNum(32434));
}
}
Related
I am trying to get a Laravel package (maatwebsite/excel) to work on CodeIgniter.
The error I get is: Message: A facade root has not been set.
Is there a way to get Laravel facades to work under CodeIgniter?
I read this article: https://www.sitepoint.com/how-laravel-facades-work-and-how-to-use-them-elsewhere/ which suggests to me that I need to call
Illuminate\Support\Facade::setFacadeApplication($app);
But what should $app be?
My code so far is this:
// Report.php
class Rapportage extends MY_Controller {
public function __construct()
{
parent::__construct();
// This doesn't work, what should I put here?
Illumiante\Support\Facade::setFacadeApplication($app);
}
public function generate_rapport()
{
// This is where the error occurs
return Excel::download(new PersonExport, 'rapport.xlsx');
}
}
// Reports/Persons.php
namespace Exports;
use Person;
use Maatwebsite\Excel\Concerns\FromCollection;
class PersonExport implements FromCollection
{
public function collection()
{
return Person::all();
}
}
My goal is to include codes from another sources which is located in resources/views. I have tried using resource_path('views/myfiles.php') but it does nothing.
Controller
class MyController extends Controller
{
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$theFilesLocation = "resources.views" . $request->input('name');
#include($theFilesLocation) //something like this
}
}
}
myfiles.php
<?php
dump("if this shows up, then the code works")
?>
Try bellow code but I think it is not a good way.
class MyController extends Controller
{
require_one(resource_path('views/myfile');
}
Or with Laravel File facade
class MyController extends Controller
{
\File::requireOnce(resource_path('views/myfile');
}
You should create a class and put your code there then call it from the controller is a better solution.
What you are looking for is a trait. This allows the easy sharing of code and functionality without having to inherit from a specific base class causing an inheritance hell.
namespace MyCode\Traits;
trait SharedCodeForThing {
public function blaTheBla() {
dump("if this shows up, then the code works");
}
}
and then in your controller
use MyCode\Traits\SharedCodeForThing ;
class MyController extends Controller
{
use SharedCodeForThing;
}
Now if you wish to just render the contents of the view which it seems you're after:
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$view = view('resources.views' . $request->input('name'));
return $view->render();//or echo $view->render(); whatever you like
}
}
I would like to create an external class in Laravel, I want to use this class in my cotroller function.
What is the best way to do that?
thanks
Create a class and define its namespace. For example:
<?php
namespace App\Services;
class MyClass
{
public function doSomething()
{
dd('It\'s working');
}
}
Run composer du command.
You'll be able to use the class in a controller with:
(new App\Services\MyClass)->doSomething();
Or with IoC:
app('App\Services\MyClass')->doSomething();
If you're using IoC, you'll also be able to inject the class into controller constructor:
use App\Services\MyClass;
protected $myClass;
public function __construct(MyClass $myclass)
{
$this->myClass = $myClass;
}
public function index()
{
$this->myClass->doSomething();
}
Used common function any file like controller,models and all blade file valid.
Please try your helpers.php file inner created.
file path like : laravel/app/helpers.php
Code
if (!function_exists('classActivePath')) {
function classActivePath($path) {
return Request::is($path) ? ' class="active"' : '';
}
}
I am new into Phalcon framework. I just got the basic idea about it. Every controller has methods with multiple specific actions. I wrote a huge indexAction method but now I want to break it down with multiple private method so that I can reuse those functionality. But when I try to create any method without action suffix, it returns error(Page Not Found). How can I break it down into multiple methods?
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
$this->someMethod();
}
public function someMethod()
{
//do your things
}
}
Controllers must have the suffix “Controller” while actions the suffix “Action”. A sample of a controller is as follows:
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
}
public function showAction($year, $postTitle)
{
}
}
For calling another method, you would use it straight forward
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
echo $this->showAction();
}
private function showAction()
{
return "show";
}
}
Docs.
What exactly do you want? The answer seems trivial to me.
class YourController extends Phalcon\Mvc\Controller
{
// this method can be called externally because it has the "Action" suffix
public function indexAction()
{
$this->customStuff('value');
$this->more();
}
// this method is only used inside this controller
private function customStuff($parameter)
{
}
private function more()
{
}
}
I have been facing a problem of not able to use the model inside the controller in the new laravel framework version 5. i created the model using the artisan command
"php artisan make:model Authentication" and it created the model successfully inside the app folder, after that i have created a small function test in it, and my model code looks like this.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
echo "This is a test function";
}
}
Now i have no idea, that how shall i call the function test() of model to my controller , Any help would be appreciated, Thanks in advance.
A quick and dirty way to run that function and see the output would be to edit app\Http\routes.php and add:
use App\Authentication;
Route::get('authentication/test', function(){
$auth = new Authentication();
return $auth->test();
});
Then visit your site and go to this path: /authentication/test
The first argument to Route::get() sets the path and the second argument says what to do when that path is called.
If you wanted to take this further, I would recommend creating a controller and replacing that anonymous function with a reference to a method on the controller. In this case, you would change app\Http\Routes.php by instead adding:
Route::get('authentication/test', 'AuthenticationController#test');
And then use artisan to make a controller called AuthenticationController or create app\Http\Controllers\AuthenticationController.php and edit it like so:
<?php namespace App\Http\Controllers;
use App\Authentication;
class AuthenticationController extends Controller {
public function test()
{
$auth = new Authentication();
return $auth->test();
}
}
Again, you can see the results by going to /authentication/test on your Laravel site.
Use scope before method name
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Mainmenu extends Model
{
public function scopeLeftmenu() {
return DB::table('mainmenus')->where(['menu_type'=>'leftmenu', menu_publish'=>1])->orderBy('menu_sort', 'ASC')->get();
}
}
above code i tried to access certain purpose to call databse of left menu
than we can easy call it in Controller
<?php
Mainmenu::Leftmenu();
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function scopeTest(){
echo "This is a test function";
}
}
Just prefix test() with scope. This will become scopeTest().
Now you can call it from anywhere like Authentication::Test().
For me the fix was to set the function as static:
public static function test() {..}
And then call it in the controller directly:
Authentication::test()
You can call your model function in controller like
$value = Authentication::test();
var_dump($value);
simply you can make it static
public static function test(){
....
}
then you can call it like that
Authentication::test();
1) First, make sure your Model is inside a Models Folder
2) Then supposing you have a model called Property inside which you have a method called returnCountries.
public function returnCountries(){
$countries = Property::returnCountries();
}
of course, in your case, replace Property by the name of your Model, and returnCountries by the name if your function, which is Test
and in the Model you write that function requesting the countries
so in your Model, place a:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return $test = "This is a test function";
}
}
and this is what your Controller will be getting
You should create an object of the model in your controller function then you can model functions inside your controller as:
In Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return "This is a test function"; // you should return response of model function not echo on function calling.
}
}
In Controller:
namespace App\Http\Controllers;
class TestController extends Controller
{
// this variable is used to store authenticationModel object
protected $authenticationModel;
public function __construct(Request $request)
{
parent::__construct($request);
$this->authenticationModel= new \App\Authentication();
}
public function demo(){
echo $this->authenticationModel->test();
}
}
Output:
This is a test function