Laravel Controller loading another controllers method :: Strange Logic Error - php

I am using Laravel 7.x, I have a controller which when called in the routes, instead of loading its own called method, will rather load the method of a different controller, I don't know how and why?
I.e. Controller-A executes Controller-B methods which was not called.
So, I am calling the route below from my web.php directory.
Route::get('/testx', 'ControllerA#show_table');
but it loads another ControllerB's method instead thereby loading the wrong route. Does laravel cache controllers? Its a strange logic error to me.

Thank you guys for your response. I finally solved the problem.
I had earlier saved a controller with the same class as the current controller while running a test, and forgot to remove it from the controller directory, this was the cause of error "Duplicate Class" in the same namespace. I simply removed the duplicate class controller and the issue was resolved.

Related

Cannot access Blade Component variables inside the view

I have been running into this very weird issue with Laravel.
I had a problem where one of my component views was not able to read the variables defined in its class.
It was kind of strange because I have several components running in my project and they all worked fine, except for this one.
So I created a fresh Laravel project to test some things out (Wanted to check if the problem was on my end, maybe I somehow messed up the project files).
I created a new component on a blank project using php artisan make:component Test
Then I simply added a test variable to the class component like so:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Test extends Component
{
public $test;
/**
* Create a new component instance.
*
* #return void
*/
public function __construct()
{
$this->test = "testing";
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\View\View|string
*/
public function render()
{
return view('components.test');
}
}
And tried to access it over in the view like so:
<div>
<p> {{$test}} </p>
</div>
For some reason, this is not working and I can't figure out why. It just says that $test is undefined.
Perhaps I should point out, I am a beginner in Laravel, so excuse me if I am making some obvious mistake. It just seemed weird that this is not working on a blank project.
Thank you in advance.
In fact, all of the answers are misleading. The $test property is already public, and as such available to the template. Please don't go and manually add parameters to the view.
I just encountered a similar issue, and while I can say for sure that it has to do with the component class name, I could not trace it down, because it started working again magically.
What I can say about it is:
I had a class with the same base classname but a different namespace, deleted the non-component class, and then suddenly the component was rendered as an anonymous component, by-passing the component class completely. (You can easily verify that by putting a dd() in your component class constructor.)
My first thought was that the autoloader needed a refresh, but composer dump did not change anything.
renaming the component class and template to something else solved the issue.
when I wanted to track down the bug, I renamed the class and template back to the original name, but now it suddenly worked...
my guess is that the view cache was causing the issue. It worked from the moment when I was changing the x-blade (<x-component-name ... />) in the template that was including the component. So it is plausible that the issue magically went away because the cached view was replaced due to the template change.
So based on this, your best options to solve the issue are:
Verify that your properties are public, they will not be available in your template if they are protected or private.
Clear caches, esp. the view cache (php artisan view:clear).
Dump the autoloader (composer dump).
Verify that your class is actually used. Put a dd() in your constructor. If you still get errors from the template, Blade is by-passing your class and trying to use your template as an anonymous component.
Rename the component to something else to see if the class name clashes.
Hope that helps anyone who experiences this confusing issue.
In my case It was because of cached view so php artisan view:clear did the job
so what is wrong?
The problem happens when you already have a view component and in some point you want to add a dedicated class to provide some data to your component and because laravel already cached your component the "dedicated class" wont be triggered
You need to send variable to the view. Some ways
In an array
$test = 'Hi';
return view('components.test', ['test' => $this->test]);
Using with
$test = 'Hi';
return view('components.test')->with('test', $test);
The last one, i like it this one.
$test = 'Hi';
return view('components.test', compact('test'));
Let me know how it works, regards.
within the render() function, instead of return view('components.test');, do return view('components.test', ['test' => $this->test])

Codeigniter Model loading improperly

Here's the thing:
I loaded the model in the constructor method. Supposedly, I could use it on all the methods inside that controller, but I can only access it properly from the index method. When I used the loaded model in a different method - same controller, it gave me an error could not get function of non-object. I can use it on index() but why not on other methods inside the same controller? I loaded the same model on different controller and it works fine. I checked my controllers to see if there's any difference between the two and make sure that they're the same - in term of syntax and such, and they're the same.
Another is that I loaded another model, used it and worked fine, it inserts, create, etc but when I used the controller method to throw a json_encoded array back to the view because I used $.ajax on that, it gives me responseText - a string not a json - and it goes directly to error function of the $.ajax. I removed the model and the $.ajaxtriggered its success function.
I don't know where's the problem here. Also it happens to not just one model and comtrollers

Action helper inclusion error + getActionController is null

I would like to write a few action helper but it seems that I am not doing it well.
First of all, in my Bootstrap file, I am doing the following:
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH.'/controllers/helpers/My/Helper/', 'My_Helper');
In this directory, I have the following tree:
My/Helper/
- Fileupload/
- Abstract.php
- Xhr.php
- Fileupload.php
In my Controller, I successfully call the fileupload helper (in the class My_Helper_Fileupload):
$res = $this->_helper->fileupload($directory);
But in the constructor of this helper, I am trying get another helper (in the class My_Helper_Fileupload_Xhr) iwth:
Zend_Controller_Action_HelperBroker::getStaticHelper('Fileupload_Xhr');
which lead me to the error
Action Helper by name FileuploadXhr not found
What am I doing wrong? I tried a lot of stuff but I can't figure out what's wrong...
Moreover, after a few test, it seems that in my constructor of the fileupload helper, I am getting NULL when I call the getActionController method. Isn't it supposed to be set automatically?
The helper broker will remove underscores as part of its process of "normalizing" the helper name you give it. It only deals with CamelCased helper names, so your Fileupload_Xhr gets converted to FileuploadXhr.
The Zend_Loader that ends up being responsible for finding and loading the right PHP file uses underscores to determine when it should add a directory separator.
If you combine these two things, the practical upshot is that you can't have a nested folder structure under any path for the action helper broker. All the helper classes you want to be able to load for any path added with addPath must reside directly under the given path, with no intervening subfolders.
The simple solution is to move your helper from My/Helper/Fileupload/Xhr.php to My/Helper/FileuploadXhr.php.
Assuming you are using My as your appnamespace - which is what I usually do; Application is too long to type - then this is one way to do it.
In Bootstrap:
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/controllers/helpers', 'My_Controller_Helper');
Name your helper class as My_Controller_Helper_SomeHelper and store it in the file application/controllers/helpers/SomeHelper.php.
Invoke in a controller using:
$this->_helper->someHelper()
If your appnamespace is different, the just replace My_ with your appnamespace.

CakePHP Manipulate more than one models in the same controller

I created two plugins in the CakePHP, and both of them have the same named model, e.g, plugin1.mod and plugin2.mod.
In the two models, defined the same named method, e.g, mymethod.
Now, I have a controller in my main program. Then, use ClassRegistry::init to initialize plugin1 and can call plugin1.mod without problem.
Problem:
When I use ClassRegistry::init to initialize plugin2 and call the plugin2.mod, it is calling plugin1.mod! Can somebody tell me what wrong here is?
Thank you.
in 2.0 this is not possible (anymore).
class paths are cached (inside App class) and therefore can use a class name only once.
You need to use different class names.

Converting from CodeIgniter 1.7.3 to 2.0+

Hello and thanks for reading.
I'll get straight to the point: I have a website project that I've been building using CodeIgniter 1.7.3, which I have thoroughly enjoyed using, however I have been contemplating upgrading to CI 2.0+.
I tried a straight copy, just moved my folders for controllers, models and views over to a CI 2.0 framework, but I got a 500 server error when I tried to view my pages.
After doing some investigation I discovered that all of your controllers must now use "CI_Controller" as their parent class. Also I noticed that if you want to include a constructor in your controller class that it must use the syntax "function __construct()" as its name and of the parent class. It seems that CI 2.0+ no longer supports using a constructor that has the same name as the class name, e.g. "class Blogs extends CI_controller{ function Blogs(){parent::__construct();}}" is no longer supported?
I've been reading the CI Change Log, but all I see are bug fixes, and new features, nothing about compatibility issues with older versions of CI?
Does anyone else know of any other secret little pitfalls?
Thanks,
H
CI 2.x removed all compatibility with PHP4 and also updated a number of standards to be compliant with PHP 5.3 going forward. One of these is the constructor issue you have encountered. As of PHP 5.3, the function ClassName() is no longer the constructor for a class, it is simply another function. You must explicitly declare a __construct function to perform any tasks that need to be done when a new instance of the class is created. Given this, you should see it no longer makes sense to call parent::ClassName() in your child constructor as that function would no longer be the parent's constructor.
Another pitfall I recently had to work out is how the $_GET array is now handled. In the 1.x versions, you could use query strings to pass back extra information and still use URI segments to route to controllers and functions. This is especially useful for AJAX calls where you may not always know all the parameters being sent to and from the server in a particular request. In the 2.x versions, the config.php file contains a new option, $config['allow_get_array']. This must be set to TRUE if you want to use query strings otherwise the input class clears out the $_GET array as part of CI's initialization routine on each request.
Something which isn't a pitfall but you may find useful is the new options in config/autoload.php that allows you to add new application directories to your project. If you work on a number of different projects with CI and want to keep any useful libraries you write in a single location, you can now add that location to $autoload['packages']. CI expects any path in this array to contain the sub-directories "controllers", "models", "libraries" and "helpers". It won't complain if you don't have those directories but you will at least need them for anything you intend to load, i.e. libraries would live in /libraries as with the main application folder.
Have you read the official guide for upgrading from 1.7.x to 2.x ?
so in short
Update Models and Controllers to
extend CI_Model and CI_Controller
Update Parent Constructor calls
class Wow extends CI_Controller {
function __construct()
{
parent::__construct();
//your stuff
}
function index()
{
// just for example
}
}

Categories