I have custom page in view/stock/three.php ( which I make) , I render it in StockController , and I try to open it from view/site/shop.php with
<div class="pager ">3</div>
but I get Not Found (#404) Page not found. . ( I didnt forget to set "use Url")
I try also to redirect it from SiteController with :
public function actionThree() {
return $this->redirect(['/stock/three']);
}
but also didnt work and still got Error code 404
maybe you don't have a 'three.php' file inside views/stock folder or that forward slash ('/') before 'stock'.
Try this
public function actionThree() {
return $this->redirect(['stock/three']);
}
I make new Model and new Controller for pages where I write :
public function actionThree() {
return $this->render('three');
}
and then
<div class="pager ">3</div>
work
Related
I get this error when i try loading blade.php
Action App\Http\Controllers\InventoryItemController#change not defined.
I have change function in InventoryItemController
public function change($new_status)
{
//
}
This started when I wanted to make button
Confirm Change
I did everything same when i made Edit button and that button works normally.
UPDATE 1
My button looks like this now
<a href="{{route('change', [$inventoryitem['new_status'],
$inventoryitem['asset_id']])}}"class="btn btn-info">Confirm Change</a>
and my change function is this
public function change($new_status, $asset_id)
{
$asset = Asset::find($asset_id);
$asset->status = $new_status;
return redirect('showasset', compact('asset','asset_id'));
}
and my route in web is like this
Route::get('change/{$new_status}/{$asset_id}','InventoryItemController#change')->name('change');
But after i click button it just redirect me to url .../change/4/1 and that's it. Nothing changes.
Using Action is deprecated in Laravel
You can use routes instead.
Define Routes in your routes files (/routes/web.php) like.
Route::get('change/{status}','InventoryItemController#change')->name('change');
and then in your view
Confirm Change
In your controller use.
public function change ($status){
// rest of the function.
}
Hope this helps
Define your controller's method in route file as following:
Route::get('url/{new_status}',InventoryItemController#change);
Answer on UPDATE 1
public function change($new_status, $asset_id)
{
$asset = Asset::find($asset_id);
$asset->status = $new_status;
$asset->save();
return view('your_view_path',compact('variable1','variable2'));
}
Final error was in my route
Route::get('change/{$new_status}/{$asset_id}','InventoryItemController#change')->name('change');
It should be like this
Route::get('change/{new_status}/{asset_id}','InventoryItemController#change')->name('change');
After that change everything is working flawlessly. Thank you for your help guys!
I Have a component ,in which have one init() function which is to be called before each action.
I only need to check if the session is expired redirect the user to login.
Here is my code,
config/main.php coponents -
'MyGlobalClass'=>[
'class'=>'common\components\MyGlobalClass'
],
and
'bootstrap' => ['log','MyGlobalClass'],
components/MyGlobalClass .php :
class MyGlobalClass extends \yii\base\Component{
public function init() {
//echo "Hi";
if(Yii::$app->user->isGuest)
{
//echo 'called';exit;
Yii::$app->getResponse()->redirect(Yii::$app->urlManager->createUrl('login'));
}
//parent::init();
}
}
so it is coming inside if{} but not redirecting to login page.
You can try this, it worked for me:
return Yii::$app->getResponse()->redirect('admin/page/signin');
You may use this to send your response
Yii::$app->response->send();
but please review the guide for controller/action distinct access control
http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
You can try the code below:
Yii::$app->getResponse()->redirect(Yii::$app->urlManager->createUrl('login'));
return;
correct way is :
Yii::$app->response->redirect(Yii::$app->urlManager->createAbsoluteUrl('site/logout'));
return;
This way you'll get an infinite redirect loop: The component is called on each controller call, redirection will never stop.
Better to manage it from public function behaviors() on each controller, check the Authorization section on Yii2 documentation.
May be this one help you :)
return Yii::$app->getResponse()->redirect(['user/login']);
or
return Yii::$app->getResponse()->redirect(Yii::$app->urlManager->createUrl('user/login'));
I am new to laravel4 .. I've made a simple website but when accessing SectionController or CompaniesController it redirects me to the SryoController while in other it works !!
that's my Route file
<?php
Route::get('/',array('uses'=>'SryoController#getIndex'));
Route::controller('admin/sections' ,'SectionsController');
Route::controller('admin/companies' , 'CompaniesController');
Route::controller('portfolio' , 'PortfolioController');
Route::controller('about' , 'AboutController');
Route::controller('contact' , 'ContactController');
?>
that's my getIndex() function in SectionController
public function getIndex () {
return View::make('sections.index')
->with('sections' , Section::all());
}
The order makes difference, try something like this, just to see what happens:
Route::controller('admin/sections' ,'SectionsController');
Route::controller('admin/companies' , 'CompaniesController');
Route::controller('portfolio' , 'PortfolioController');
Route::controller('about' , 'AboutController');
Route::controller('contact' , 'ContactController');
Route::get('/',array('uses'=>'SryoController#getIndex'));
Check your urls, the routes are correct, and you might have something in the filters.php that performs the redirect, or maybe have you changed your 404 view
I encounter problems when I call the method of a controller. By the way, this controller is routed.
Routes
$route['admin/company'] ='company';
Controller
class Company extends CI_controller {
public function __construct() {
parent::__construct();
session_start();
/** Check if user is logged in */
if ($this->session->userdata('user') != "") {
$this->load->model('my_model');
if ( $this->uri->segment(1) != "admin" ) {
redirect('admin/company/'.$this->uri->segment(2));
}
} else redirect('/');
}
public function index() { Some coding here............ }
public function addnew() { Some coding here...........}
public function process() { Some coding here...... }
}
When I call "localhost/company", it works fine and redirects me to "localhost/admin/company which is great. But, when I try to call the method of it, it displays a 404 error message.
Example: When I go to link: localhost/admin/company/addnew
Did lack something in routes? or in controller? or anything else?
Thanks,
James
If appropriate for all use cases, use a simple catch-all rule in routes.php:
$route['admin/company/(.+)$'] = "company/$1";
You will have to add a route for each function in your controller.
$route['admin/company/addNew'] ='company/addNew';
$route['admin/company/process'] ='company/process';
It's very annoying. Better, create a folder "admin" inside your "controllers" folder. Put the controller on the folder. Thus you can access your controller with the URL "localhost/admin/company" and all the methods without rerouting.
If it doesn't work at first, create a controller inside "admin" folder with the same name you'll find in your routes file (default_controller).
create a admin directory and add $route["company"]="admin/company"
i was create codeigniter controller like this :
function Home() {
parent::Controller();
if(!$this->session->userdata('id'))
redirect ('login');
$this->load->model('Home_Model');
}
but when i try to access the page, the redirect page now show. just show 404 not found but i was create login.php on controller and views also home_model.php on model.
what the problem with that code and how to fix that?
thank you
Because of this line: parent::Controller(), I assume that you are using CodeIgniter 1. Since you are building pages like home and login, I assume that you are starting building a new website. If my assumptions are right, I suggest you using the latest CodeIgniter (v2.1.0 at the moment, required PHP 5.1.6 or newer). You can grab a copy at CodeIgniter Home Page.
First of all, I think you should check your error reporting level. It should be something like error_reporting(E_ALL); for debugging. If not yet, turn it on and run the code again. Maybe you will get your own answer after that :)
After that, you should try the code below for your constructor to check that your home controller process as you expected.
function Home()
{
parent::Controller();
echo 'Home Class Initialized';
}
It should at least show 404 page. If it doesn't print out anything then we need more information. Maybe you should turn on logging and analyze your log files: look for /application/config/config.php: $config['log_threshold'] = 2;
If it print out 'Home Class Initialized' follow by a 404 message: please check if your controller have something like:
public function index()
{
echo 'Invoke default method';
}
If not yet, please add one. You may also want to take a look at using your own default method here
If it still a 404 then sorry, I have no idea. Maybe you should post more code.
If it print out 'Home Class Initialized' only: good. Now try this
function Home()
{
parent::Controller();
if ( ! $this->session->userdata('id'))
{
echo 'Redirecting to login page';
//redirect ('login');
}
$this->load->model('Home_Model');
}
If this print out Redirecting to login page, it's now time for you to uncomment //redirect ('login'); and check the login page. Follow the same path with step 1 & 2.
If not, check your session data: var_dump($this->session->userdata('id'). You could learn more about PHP falsehood here
After all, if it still fails, consider using __construct() of PHP 5
function __construct()
{
parent::__construct();
if ( ! $this->session->userdata('id'))
{
redirect ('login');
}
$this->load->model('Home_Model');
}
Other: if you use this $this->load->model('Home_Model');, I think you will have to call home_model like this $this->Home_Model->foo(); or there will be error access to NULL