i just want to run $CI=&get_instance in Exception library of codeigniter. but its not running there. while the other Classes in Library folder are able to create such instance, but the Exception is not. the reason i want to do this is , i want to use this instance to load a view page in show_404(){} method.
thanks in advance
Are you thinking about extending CI's Exceptions library? if so, you may want to do
<?php
class MY_Exceptions extends CI_Exceptions {
// your code here
}
then you don't need to do $CI &= get_instance();
Related
I want use a variable from my database to set a folder for custom views.
"class AppServiceProvider extends ServiceProvider"
$ActiveProject = ThemeConfig::where('module_type',"project")->where('active',"1")->first()->file;
After this, I get an active project name (like Nshop), and I want to set it in:
public function register()
{
$this->app['view']->addNamespace('Projects', base_path() . '/Projects/'.$ActiveProject.'/Views');
}
But I get an error.
How can I accomplish this task?
Using ORM Models in the AppServiceProvider doesn't work. This file is part of the boot process of Laravel where your models are not loaded yet. But you can rely on functions that are part of the Laravel core concept.
$ActiveProject = ThemeConfig::where('module_type',"project")->where('active',"1")->first()->file;
Becomes
$ActiveProject = \DB::table('theme_configs')->where('module_type',"project")->where('active',"1")->first()->file;
This is the test case I've written and when i try to run it with this command vendor/bin/phpunit on linux it gives me the error "Call to undefined method Tests\Unit\ExampleTest::visit()"
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$this->visit('/Login')
->type('KP123#gmail.com','email')
->type('123456','password')
->press('Login')
->seePageIs('/home')
->see('Katy Perry');
}
}
I've tried running composer update and it still could not work. Had Anyone experienced this issue before?
The visit() method looks like it's part of the SeleniumTestCase. So my guess is you either extend from the wrong base class. I assume it should look something like this:
class ExampleTest extends \PHPUnit_Extensions_Selenium2TestCase
{
// your test method
}
edit: I just noticed the Laravel tag, so it's probably more something like Illuminate\Foundation\Testing\TestCase. If this gives you errors that it can't find the class you have to set the bootstrap file in your phpunit.xml to the vendor/autoload.php or another appropriate bootstrap file where the file is registered in the autoloader.
I might have come in late onto this question but here is my 2 cents. I have been trying to use the same methods with my test case which led to a dismal failure.
I the realised that I could actually make use of the TestResponse class which is found on the Foundation namespace that wraps many methods around the response object.
$this->get('/url/to/visit'); //Then you can make your assertions on the returned response.
In my case I had authentication turned on for the application therefore it always complained about the header view where we display the username of the currently logged in user. If you run into that situation then use the Auth Facade like shown below:
\Auth::loginUsingId(1);
You should now be able to assert that your response has some text in it. Hope this helps. By the way I am on Laravel 5.4
If you are using new version of Laravel you must use Laravel Dusk to use similar methods to $this->visit or see. It was excluded from Laravel around version 6.
Instead you can use, but it has limited options:
$this->get('/Login')
composer require laravel/browser-kit-testing --dev
Try this
if you are using > Laravel 8 try :
$this->get('/Login')
instead of visit function, and assertSeeText instead of see
I am using Laravel 5.4.16 While using make method of View class i am getting undefined method error.
public function Index()
{
return View::make('stats');
}
Try to add this to the top of the class:
use View;
If it doesn't work then it looks like you didn't install the project. You need to run composer install or composer update command which will download and install all dependencies into the vendor directory.
Another thing to check is config/app.php should have this line:
'View' => Illuminate\Support\Facades\View::class,
Alternatively, you could use helper:
return view('stats');
But it will work only if porject is installed properly.
return view('path.to.your.directory');
You are receiving this error because make() method doesn't exist anywhere in your code. If you want to go from controller to view you can use simply:
public function Index() {
return view('stats');
}
It will go to your status blade view by accessing your url request.
Just use Facade instead. Check how it works and then check what is wrong with View first. And I hope you are not using another template engine as Twig for example, cause if yes then you have definitely use Facade or check how the View factory is working, I think it requires template engine in the constructor ...
Just use use Illuminate\Support\Facades\View;
You can try to use it:
return view('stats');
It is working correctly with MX_Controller.It is throwing error as 'Fatal error: Class 'CI_Template' not found' when using REST_Controller.Any help will be appreciated.
I'm not entirely familiar with the inner workings of Codeigniter's third party add-on HMVC. However, the code base that I inherited is using it.
I ran in to the same issue when installing restserver, https://github.com/chriskacerguis/codeigniter-restserver which is what I assume you were also doing.
REST_Controller.php has this
abstract class REST_Controller extends \CI_Controller {
While I have never been able to get CI_Controller to work with this codebase, I now believe it is because of this HMVC. For those not familiar, HMVC is Hierarchical Model View Controller.
Module Controllers can be used as normal Controllers or HMVC
Controllers and they can be used as widgets to help you build view
partials.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
I did get REST_Controller.php working by changing the line to
abstract class REST_Controller extends \MX_Controller { // \CI_Controller {
I am working with codeigniter in php and my question is that How can i Call an external controller in a controller located in controllers folder.
//this file is Controller2.php in controllers folder
class Controller2 extends CI_Controller
{
function one()
{
// Some code goes here..
}
}
//and now this file is Controller1.php which is also in controllers folder
class Controller1 extends CI_Controller
{
function one()
{
// I want to load Controller2 here
}
}
Please instruct me as i've wasted so much time during googling.
Thanks in advance..
Kamran
You can, but not with the default functionality, to use something similar you should use HMVC extension for CodeIgniter: Link
There are plenty examples and a good thread about the extension.
You can't, or rather you shouldn't. Any functionality that you need to use in two different controllers should be moved to a library or helper file.