How to get Class Method on Laravel - laravel

Hello I try to get all Methods on a Class Controller on Laravel but I was failed.
I use get_class_methods function to get class methods.
Here is my ConfigController codes:
use Modules\Order\Http\Controllers\V1\Web\OrderController;
class ConfigController extends Controller
{
public function index(){
$methods = get_class_methods(new OrderController::class);
dd($methods);
}
}
After executing the code the result is:
Class "Modules\Order\Http\Controllers\V1\Web\OrderController" not found
And I'm sure that that class exists on that namespace because I have already called it on route.
How to do that on the right way?
Thank you.

Have you tried reflection yet? My example below:
$reflection = new ReflectionMethod($className, $methodName);
$parameter = $reflection->getParameters();
foreach ($parameter as $param) {
echo $parameter->getName();
echo $parameter->isOptional();
}

This is the answer, I just miss s on the method name:
use ReflectionClass;
...
$reflection = new ReflectionClass(OrdersController);
dd($reflection);
...

Related

How can i get current module name [nwidart/laravel-modules]

How can i get current module name, or current namespace. of laravel
I use this library [nwidart/laravel-modules].
I trid this code but not solve problem
$module_name = basename(__FILE__, '.module');
To get module entity: $module = Module::find('blog');
To get module name: $module->getName();
This is documented pretty well by package author here:
https://nwidart.com/laravel-modules/v1/advanced-tools/module-methods
Add this to your modules __construct controller :
private $module_name;
public function __construct()
{
$class = get_called_class(); // or $class = static::class;
$arr_class = explode("\\", $class);
$this->module_name = $arr_class[1];
}
public function index()
{
echo $this->module_name;
}

Object could not be converted to string (laravel)

My question is, why does the following not work?
$questions = Question::where($queryatypes);
$questions->get();
I'm getting the following Error:
Object of class Illuminate\Database\Eloquent\Builder could not be
converted to string
Please check the answer
$questions = Question::where($queryatypes);
$questions = $questions->get();
I think you are trying this on your controller and return this Objects directly
use Illuminate\Http\Response;
public function controllerFunction(){
$queryatypes = .....
$questions = Question::where($queryatypes);
$questions->get();
return response($questions);
}

laravel route by passing parameter got page not found

I just regular do the route with passing parameter
Route::get('cabinet', 'CabinetController#index');
Route::get('cabinet/{$id}', 'CabinetController#show');
and the controller just simple like this
class CabinetController extends Controller
{
function index()
{
$cabinets = Cabinet::all();
return view('detail', compact('cabinets'));
}
function show($id)
{
$single = Cabinet::find($id);
$cabinets = Cabinet::all();
return view('detail', compact('cabinets', 'single'));
}
}
public/cabinet/1
How come i got
Sorry, the page you are looking for could not be found.
Thank you for solve this for me
Remove the $ from route declaration:
Route::get('cabinet/{id}', 'CabinetController#show');

Laravel How to get Route Profile based on Route Name

I have this route:
Route::get('/test',['as'=>'test','custom_key'=>'custom_value','uses'=>'TestController#index'])
I've been tried to use $routeProfile=route('test');
But the result is returned url string http://domain.app/test
I need ['as'=>'test','custom_key'=>'custom_value'] so that I can get the $routeProfile['custom_key']
How can I get 'custom_value' based on route name ?
For fastest way, now I use this for my question:
function routeProfile($routeName)
{
$routes = Route::getRoutes();
foreach ($routes as $route) {
$action = $route->getAction();
if (!empty($action['as']) && $routeName == $action['as']) {
$action['methods'] = $route->methods();
$action['parameters'] = $route->parameters();
$action['parametersNames'] = $route->parametersNames();
return $action;
}
}
}
If there's any better answer, I will be appreciate it.
Thanks...
Try this:
use Illuminate\Support\Facades\Route;
$customKey = Route::current()->getAction()['custom_key'];
I believe you are looking for a way to pass variable to your route
Route::get('/test/{custom_key}',[
'uses'=>'TestController#index',
'as'=>'test'
]);
You could generate a valid URL like so using
route('test',['custom_key'=>'custom_key_vale'])
In your view:
<a href="{route('test',['custom_key'=>'custom_key_vale'])}"
In your controller method:
....
public function test(Request $request)
{
$custom_key = $request->custom_key;
}
....
You can try one of the below code:
1. Add use Illuminate\Http\Request; after namespace line code
public function welcome(Request $request)
{
$request->route()->getAction()['custom_key'];
}
2. OR with a facade
Add use Route; after namespace line code
and use below into your method
public function welcome()
{
Route::getCurrentRoute()->getAction()['custom_key'];
}
Both are tested and working fine!

RedBean ORM and Codeigniter, how to make Fuse recognize models loaded from CI default models path?

Codeigniter has its own Models path, where models extend from CI_Model. I'm using RedBean has a library in Codeigniter, loading it on a controller. After loading Rb, I try to use CI Loader to load a model that extends redbean_simplemodel (wish works, there's no error), but the events / methods inside the model have no effect when they're called on bean.
For example,
APPPATH/application/libraries/rb.php
class Rb {
function __construct()
{
// Include database configuration
include(APPPATH.'/config/database.php');
// Get Redbean
include(APPPATH.'/third_party/rb/rb.php');
// Database data
$host = $db[$active_group]['hostname'];
$user = $db[$active_group]['username'];
$pass = $db[$active_group]['password'];
$db = $db[$active_group]['database'];
// Setup DB connection
R::setup("mysql:host=$host;dbname=$db", $user, $pass);
} //end __contruct()
} //end Rb
And then on
APPPATH/application/models/model_song.php
class Model_song extends RedBean_SimpleModel {
public function store() {
if ( $this->title != 'test' ) {
throw new Exception("Illegal title, not equal «test»!");
}
}
}
while on
APPPATH/application/controllers/welcome.php
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('rb');
$this->load->model('model_song');
$song = R::dispense('song');
$song->title = 'bluuuh';
$song->track = 4;
$id = R::store($song);
echo $id;
}
}
My question is, how to make RedBean (FUSE http://redbeanphp.com/#/Fuse) work on Codeigniter ?
Thanks for looking!
----- FOUND SOLUTION!
Actually, it's working! I was trying to place code under my model, method store(). That wont work! I tryed to place a new method called update() and it does work! Check the example below:
class Model_song extends RedBean_SimpleModel {
public function update() {
if ( $this->title != 'test' ) {
throw new Exception("Illegal title!");
}
}
}
The solution is the following:
"Assuming that you've already installed RedBean on Codeigniter"
1) Load the library for «redbean»
2) Using ci_loader, load the desired model (the model must extend redbean_simplemodel)
Thanks for looking! I hope this helps other people too.
The solution is the following:
"Assuming that you've already installed RedBean on Codeigniter"
Load the library for «redbean»
Using ci_loader, load the desired model (the model must extend redbean_simplemodel)

Resources