Multiple laravel 5.5 routes methods showing error - php

I have three routes in web.php
Route::get('/secondary', 'SecondaryController#show');
Route::get('/primary', 'PrimaryController#show');
Route::get('/nursery', 'NurseryController#show');
But when i click on the respective menu link, it presents the first route, others just wont work.
pls what am i doing wrongly, i need help.
this is code for the controllers
for the nursery controller
public function show($slugs){
$NurseryPages = NurseryPages::findByURL($slugs);
return view('nursery.show', ['NurseryPages' =>$NurseryPages]);
}
for the primary controller
public function show($slugs){
$PrimaryPages = PrimaryPages::findByURL($slugs);
return view('primary.show', ['PrimaryPages' =>$PrimaryPages]);
}
for the secondary controller
public function show($slugs)
{
$SecondaryPages = SecondaryPages::findByURL($slugs);
return view('secondary.show', ['SecondaryPages' =>$SecondaryPages]);
}
it will only work well for the nursery section, but on others it displays error: trying to get object of non-property and refers me back to the nursery.show file
this is the error msg
ErrorException (E_ERROR)
Trying to get property of non-object (View: C:\xampp\htdocs\acadapp\resources\views\secondary\show.blade.php)
<?php echo $__env->make('inc.secondary.navbar', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<div class="container">
<div class="row">
<div class="col-md-12">
<br/><br/><br/>
<b><h3><?php echo $SecondaryPage->title; ?></b></h3>
<?php echo $SecondaryPage->body; ?>
</div>
</div>
</div>
<?php echo $__env->make('inc.secondary.footer', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

I think you need to change your route like:
Route::get('/secondary/{slug}', 'SecondaryController#show');
Route::get('/primary/{slug}', 'PrimaryController#show');
Route::get('/nursery/{slug}', 'NurseryController#show');

Related

Form action with class and method adds to existing url

I have sign in form which has URL - "http://localhost/ci/signin". After click on submit, it goes to - "http://localhost/ci/login/userLogin", which affects hyperlink opening problem because it searches for that page in 'login', which is actually on "http://localhost/ci/home".
How to solve this problem?
my signin form page code is:
<?php echo form_open('login/userLogin'); ?
<div class="top-margin">
<label>Email <span class="text-danger">*</span></label>
<?= form_input(['name'=>'email','class'=>'form-control']); ?>
</div>
<div class="top-margin">
<label>Password <span class="text-danger">*</span></label>
<?= form_password(['name'=>'password','class'=>'form-control']); ?>
</div>
<hr>
<div class="row">
<div class="col-lg-8">
<b>Forgot password?</b>
</div>
<div class="col-lg-4 text-right">
<?= form_submit(['name'=>'submit','value'=>'Sign in','class'=>'btn btn-action']); ?>
</div>
</div>
</form>
My "login" controller code is:
class Login extends MY_Controller
{
public function userLogin()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('password','Password','required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");
if($this->form_validation->run() == FALSE)
{
$data['title'] = ucfirst('signin'); // Capitalize the first letter
$this->load->view('user/header', $data);
$this->load->view('user/nav', $data);
$this->load->view('user/signin', $data);
$this->load->view('user/footer', $data);
}
else
{
echo "Success";
}
}
}
?>
Routes:
$route['(:any)'] = 'user/view/$1';
$route['default_controller'] = 'user/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
If I understand your question and your code correctly (never used codeigniter), you have the form loading from the login directory.
<?php echo form_open('login/userLogin'); ?
What happens when you try:
<?php echo form_open('home/userLogin'); ?
Hope this helps! :)
I don't use CodeIgnite myself, but it probably has helper functions which can correctly output an URL.
In Laravel it is:
url('this/url/is/not/added/to/the/current/url');
Even when you're at 'localhost:8000/somepage' this outputs:
"localhost:8000/this/url/is/not/added/to/the/current/url"
It is a PHP (helper)function, so output it in PHP.
Yes. I found solution of my problem. It was my mistake actually. Hyperlinks was not working because i didn't write full address for that hyperlinks.
Previously, My hyperlinks was-
Home
Now, I changed it to...
Home
Now, It Works. Enjoy.

how to load view in codeigniter with variable?

hi i want to just load the view page using variable not by direct name
here is my code:-
function load_views()
{
$this->load->model('test_model');
$data=$this->test_model->getMenu();
foreach($data as $data_menu) {
echo $data_menu->views;
$this->load->view($data_menu->views);
}
}
output :
test_view
An Error Was Encountered Unable to load the requested file: .php
actually it takes the value from db but it did not call the view file which is present in the view folder.
this is my view page :
<div class="panel-heading">
<h3 class="panel-title">Dashboard</h3>
</div>
<br/>
<div class="row">
<?php
$count = 0;
foreach($m as $data_menu){
if(($count% 4== 0))
{
?>
<div class = "row"></div>
<?php
}
?>
<div class="col-md-3">
<!--<a href="<?/*php echo base_url()*/?>index.php/<?php/* echo $data_menu->views*/?>">-->
<a href="<?php echo base_url()?>index.php/dn_login_controller/load_views">
<img class="img-rounded" src="" height="80" width="80" />
<div class="caption">
<h3><?php echo $data_menu->function_name; ?></h3>
</div>
</a>
</div>
<?php
$count++;
}
?>
</div>
</div>
Now in the view i want load views dynamically but i cant...
please help me.....
If you var_dump($data) you will see that it has an empty value somewhere in the array.
As kumar_v said in a comment, try to check for empty values
foreach($data as $data_menu) {
if(!empty(trim($data_menu->views)))
$this->load->view($data_menu->views);
}
This issue might be because, you cannot just directly load multiple views from single function/controller, when you just load view CI will send it to browser.
There are two other workaround for this :
Pass 2nd parameter NULL & 3rd parameter TRUE while loading your menu views, which will create your html as data instead of sending it to browser. Refer this link : codeigniter: loading multiple views
Create a view file, pass your data to that view & on that view execute for loop & load all your views.

Display two views at once and change url on change

I have a main view with a menu which helps me display another view. It's similar to this:
<div id="page">
<div id="menu">
Page1
Page2
</div>
<div id="content">
<!-- Page1 or Page2 are displayed here -->
</div>
</div>
I'm using php's Yii framework. Which makes me not to use <?php include("menuview.php"); ?>. So I'm looking for a different solution. I can do this with Ajax, but I would also like the link to change to mypage/controller/Page2. With Ajax I can only get it to this: mypage/controller/index#Page2
in main view, instead of include do
<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>
UPDATE:
protected/views/controller/page1.php and protected/views/controller/page2.php content at your liking
protected/views/layouts/custom.php:
<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
<div id="menu">
<?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
<?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
</div>
<div id="content">
<?php echo $content; ?>
</div>
</div>
<?php $this->endContent(); ?>
protected/controllers/ControllerController.php:
class ControllerController extends Controller {
/**
* #var string the default layout for the views.
*/
public $layout = '//layouts/custom';
public function actionPage1() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page1');
else
$this->render('page1');
}
public function actionPage2() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page2');
else
$this->render('page2');
}
}
UPDATE2:
If you need the link in address bar to change too then your only option is to use regular link and not ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>
using ajax the preferred way is using hash like you mentioned.

Yii display and validation of fields not present in table

I am a newbie in Yii. I have created a page where user can change their password.
So in my changePassword view I have :
<div class="row">
<?php echo $form->labelEx($model,'oldpwd'); ?>
<?php echo $form->textField($model,'oldpwd'); ?>
<?php echo $form->error($model,'oldpwd'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pwd'); ?>
<?php echo $form->textField($model,'pwd'); ?>
<?php echo $form->error($model,'pwd'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pwd_repeat'); ?>
<?php echo $form->passwordField($model,'pwd_repeat'); ?>
<?php echo $form->error($model,'pwd_repeat'); ?>
</div>
Now obviously I am getting an error as only the field 'pwd' is in the table and thereby in the model. I am new to MVC frameworks and can use some help here. Thanks
Declare them in your model as property of model First..
public $old_pwd;
public $pwd;
public $pwd_repeat;
As you are asking model Labels of these attributes..define them in your attributeLabels function in model..
public function attributeLabels()
{
return array(
'old_pwd'=>'Old Passw....',
'.....same way for all those who are not already there..'
);
}
Declare them safe in rules if required...
add the following line to your model
public $old_pwd;
public $pwd_repeat;
we instruct Yii to use this field as virtual field instead of searching in database field

I'm forced to call a controller method from a view, how do I refactor my code?

Okay so, I'm working with CodeIgniter. posts.php is my view that displays all the posts, each post must display its corresponding comments, which is what I'm trying to achieve.
I have a method in my model that takes the postid($postid) and return its corresponding comment($comment), unless I call the model method via a controller method,how do I accompolish this?
This is my view :
<body>
<?php foreach ($post as $key):?>
<div class="container">
<div class="span10">
<div id="box" class="alert-message block-message info">
<div id="post" class="post">
<?php echo $key->content;?><br />
</div>
<div>
<p><?php //echo $comment;?></p> <!--HERE THE COMMENTS OF THE CORRESPONDNING POST MUST BE ECHOED-->
</div>
<div>
<p><em>Comment</em></p>
</div>
<div id="commentarea<?php echo $key->postid;?>">
<?php $name=array('name'=>"form$key->postid");
echo form_open("/welcome/comments/$key->postid",$name);
$data=array(
'id' => 'input',
'name'=> 'content',
'rows' => '2',
'placeholder' => "Write a comment...",
'autofocus' => 'TRUE'
);
echo form_textarea($data);
?>
Comment
<?=form_close();?>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function(){
$("div#commentarea<?=$key->postid;?>").hide();
$('a#commentnow<?=$key->postid;?>').click(function(){
$("div#commentarea<?=$key->postid;?>").slideToggle(250);
});
});
</script>
<?php endforeach;?>
</body>
This is my controller method that returns the comments that corresponds to the postid:
public function comments($postid)
{
//Post Comment
$comment=$this->input->post('content');
$data=array('content'=>$comment,'comment_postid'=>$postid);
$this->comments->postcomment($data);
//Retrieve
$comments['comment']=$this->comments->retrieve($postid);
$this->load->view('posts',$comments);
}
I'm a newbie,pardon me if my code is bad. I'm always looking forward to improving my code> Thanks for being patient. :)
Looking at your code it seems to me that you haven't fully understood how to use the MVC.
Firstly, your controller method comments contains the extraction of comments AND adding comments to a post. This doesn't seem logical when taking a look at the view file.
Instead you should seperate those two.
In your controller, add another metod called *post_comment* and move the adding comment functionality to that method and add a redirection afterwards:
public function post_comment($postid)
{
//Post Comment
$comment=$this->input->post('content');
$data=array('content'=>$comment,'comment_postid'=>$postid);
$this->comments->postcomment($data);
redirect('welcome/comments'); //redirect back to comments
}
Now, remove the adding of a comment from your comment method in the controller, so that you only retrieve the comments:
public function comments($postid)
{
//Retrieve
$comments['comment']=$this->comments->retrieve($postid);
$this->load->view('posts',$comments);
}
And finally change your view file - you need to post the comment to a new URL:
<?php $name=array('name'=>"form$key->postid");
echo form_open("/welcome/post_comment/$key->postid",$name);
This should do the trick.

Categories