Yii 2.0 static link to a controller - php

I'm very new to Yii 2.03 (and frameworks in general). I'm facing some problem now.
On the main page I have a menu on which I want to place (in the view) the link to the controller, but I don't know how, I'm getting " PHP Fatal Error – yii\base\ErrorException
Class 'CHtml' not found" or " PHP Fatal Error – yii\base\ErrorException
Class 'Html' not found"
http://localhost/web/index.php is the main page with the menu.
http://localhost/web/index.php?r=autori/index is the page with the generated CRUD, which works perfectly.
In the view I have <li>Autori</li> and I want to replace the '#' with a valid link, I don't care for SEO now. How can I edit the view to use the specific controller?
Thanks!

If you are indeed using Yii2, then try using:
<li>Autori</li>
You'll need to include this in the beginning of your view file, so it uses the correct namespace for the Url helper class:
<?php use yii\helpers\Url ?>
But pay attention to the framework version, as Sliq noted, CHtml is a Yii1 class.

Related

Showing header and footer in CodeIgniter custom error pages

In my CodeIgniter folder I have got custom error pages for each type of error in application/views/errors/html like error_404.php etc. So when I tried to update the layout and added <?php $this->load->view('header'); ?> on top then it started to show me errors like:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Exceptions::$load
Filename: html/error_404.php
So I want to know how can I make it to work? I also tried to extends the Exception class from CI_controller but still no luck. Please help me to add my custom header to my custom error page. Thanks.
PS: I want to clear that even if I use some how PHP's include() function but I also want to use base_url function for e.g.
<script src="<?php echo base_url('assets/js/iconmenu.js'); ?>" type="text/javascript"></script> <br>
One more thing I want to clear that I am using Codeigniter 3.0 which provides all error's templates but don't allow to use $this->load function.
I don't know the reason for the above error. Might be it's because of core libraries are not loaded in case error 404 is happened.
You can try another way. See application/route.php and there is an option to override the 404 url, $route['404_override'] = '';
Example: Create a method error_404 in Welcome controller and update the route variable like $route['404_override'] = 'welcome/error_404';, now you can use whole helpers / libraries etc.
See routes
Hope this will help!

Getting an error when trying to add component to AppController in CakePHP

Im trying to add a SessionComponent to my controller in order to able to change the language of my app on the fly. The following snippet (specifically line 3) is the code i've tried according to http://book.cakephp.org/2.0/en/controllers/components.html#using-components
class AppSettingsController extends AppController
{
var $components = array('Session');
But when i try to run any of the actions of my Controller i get:
SessionComponent could not be found.
Create the class SessionComponent below in file:
src/Controller/Component/SessionComponent.php
As if the SessionComponent doesn't exist. All answers i have been able to find say that what i have already done ought to work. Do you have any idea what i might have missed or what i should look into to fix it?
From your post I conclude that you are referred to a CakePHP 2 documentation, but error message telling us that you are using CakePHP 3!
src/Controller/Component/SessionComponent.php
cakephp 3 has the following components:
Authentication
Cookie
Cross Site Request Forgery
Flash
Security
Pagination
Request Handling
and here is how to use sessions in your application:
http://book.cakephp.org/3.0/en/development/sessions.html#accessing-the-session-object

Silverstripe get the values in Security login page

I am using SS 3.02 and have made a lots of modification in the core files. I am facing the issue that I am trying to set the color of the navigation background dynamically. This works fine for pages other than security/login page. Suppose I am getting the value in $navbgcolor, this shows up well on home page or about us page or any other page. But this does not show up on the Security/login page. Any help would be greatly appreciated. Thanks.
Firstly, it is never a good idea to alter the core files as this prevents you from easily updating your version of SilverStripe. You could miss out on bug fixes and important security updates.
The reason this isn't working on the login page is because the login page works from the Security controller which directly extends Controller. Your code (presumably in Page_Controller) will be completely bypassed.
Here is a way you could apply your code to all controllers, without touching the core:
<?php
class MyControllerExtension extends Extension {
public function onAfterInit() {
//... Your code here...
}
}
In your config file you would apply your new controller extension to Controller.
If you're using _config.php
Object::add_extension("MyControllerExtension", "MyControllerExtension")
If you're using YAML (recommended)
Controller:
extensions:
- 'MyControllerExtension'
You can learn more about extensions here: http://doc.silverstripe.org/framework/en/reference/dataextension
Also to let you know, you can create specific template file for the Security login pages by creating action sub-templates. Example is if you created a file in your theme called "Security_login.ss" you can call in variable, change the mark up etc.
Note the convention here is the filename is called the name of the class in this case "Security" then "_" followed by the name of the action to be rendered by your controller ("login" in this case).
As mentioned by micmania1, the golden rule for developing in SilverStripe is...
"Don't hack the core or modules!"
Instead as pointed out use extensions to decorate classes, or use subclasses if you have to.

OpenCart - 404 page not found error with new module

OpenCart 1.5.6; Theme: Default.
I've created a new module 'Seller' cloned by 'Manufacturer' ... Admin side & Front side is working okay, means adding / editing / saving data is working okay, except an issue.
When I click on 'Seller' link at product page (frontend), it shows 404 error / page not found, ideally it should work same as Manufacturer module works, should open seller's page with list of products by the same seller.
What could be the reason? Because I don't see any error in log files / VQMOD, it just shows 404 error.
Any clue?
Probably this happened here:
Let's assume Your seller URL is http://my.domain.com/index.php?route=product/seller&seller_id=1.
Now the route part product/seller tries to load this controller file:
catalog/controller/product/seller.php
^^^^^^^^^^^^^^
and while there is no action specified (which would be e.g. product/seller/showList) the index action is called. This all means, that You need to have the above mentioned file, which has to contain a class ControllerProductSeller extending from Controller and this class has to have method index implemented.
This would look like
class ControllerProductSeller extends Controller
{
public function index()
{
// ...
}
// ...
}
After this is fulfilled You should not receive a 404 error.
I can see You are completely new to OpenCart and new modules creation (reminds me of my starts) and my advice is: look and discover how the things in OpenCart are done, copy+paste+rename wisely. Most of errors like this (and missing template, language, model files, undefined method names etc) are caused by improper renaming, or in other words, by hot head and quick fingers... Slow down and start thinking a little bit about what are You doing and what needs to be done.

Not able to add a link in code igniter

I am a beginner to Wamp server. I am trying to design a website with project name as "helloall" in netbeans IDE.
In the views folder I have two files layout1.php and layout2.php.
I am trying to call layout2.php from layout1.php in the below style.
<div id="logo"> <span>LAYOUT2</span> </div>
But I am facing the below error, for which I am not able to find the reason.
The requested URL /helloall/layout2.php was not found on this server.
Do I need to change anything in the configuration? I am using all the default configurations.
as per Codeigniter standard you have to follow the MVC pattern so:
Model -> Controller ->view
now, assuming you want to visualize layout2.php view you have 2 chances:
1 - load view directly where you need $this->load->view('layout2');
2 - create url function ad hoc kind of www.site.com/layout/layout1 and www.site.com/layout/layout2:
controller layout.php
class Layout extends CI_Controller {
function layout1(){
$this->load->view('layout1');
}
function layout2(){
$this->load->view('layout2');
}
}
i really suggest you to look at How To Create a Controller in Codeigniter Doc

Categories