Laravel 4 - ReflectionException Class / does not exist - reflection

When attempting to load the page, I'm getting the error that the ReflectionException Class / does not exist (open: /var/www/laravel_guestbook/vendor/laravel/framework/src/Illuminate/Routing/ControllerInspector.php), could use some insight on what is causing this error.
Furthermore, I've also run 'composer dump-autoload' at the root of my project folder to no avail.
routes.php
Route::controller('EntriesController', '/');
Entry.php
<?php
class Entry extends Eloquent {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'entries';
}
?>
home.blade.php
<html>
<head>
<title>Laravel 4 Guestbook</title>
</head>
<body>
#foreach ($entries as $entry)
<p>{{ $entry->comment }}</p>
<p>Posted on {{ $entry->created_at->format('M jS, Y') }} by
{{ $entry->username}}
</p><hr>
#endforeach
<form action="/" method="post">
<table border="0">
<tr>
<td>Name</td>
<td><input type="text" name="frmName" value="" size="30" maxlength="50"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="frmEmail" value="" size="30" maxlength="100"></td>
</tr>
<tr>
<td>Comment</td>
<td><input textarea name="frmComment" row="5" cols="30"></textarea></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</td>
</tr>
</table>
</form>
</body>
EntriesController.php
<?php
class EntriesController extends BaseController {
# Handles "GET /" request
public function getIndex()
{
return View::make('home')
->with('entries', Entry::all());
}
# Handles "POST /" request
public function postIndex()
{
// get form input data
$entry = array(
'username' => Input::get('frmName'),
'email' => Input::get('frmEmail'),
'comment' => Input::get('frmComment'),
);
// save the guestbook entry to the database
Entry::create($entry);
return Redirect::to('/');
}
}
?>

It's suppose to be:
Route::controller('/', 'EntriesController');

If your naming is correct but you still get this type of error do a
composer update
This command will refresh your composer autoload files (among others).

In my case the name of the file was PostController.php, but inside I had
class Post extends \BaseController {
Instead of
class PostController extends \BaseController {
I had to rename the file as "php artisan generate:controller " command requires the word controller to be specified.

Related

Thymeleaf form submit with model having associations

The application is web-application using spring boot and thymeleaf. The application has a questionnaire along with some fields that needs to filled to submit the form. The model object (here answeredQuestionnaire) in template th:object="${answeredQuestionnaire}" has nested objects with as shown below.
When I submit the form, I need to assign the questionId and possibleAnswerId to answers (Set) in answeredQuestionnaire. How can I assign this in thymeleaf template. Please provide some suggestions.
AnsweredQuestionnaire .java
public class AnsweredQuestionnaire {
private UUID id;
private Boolean isRirelevant;
private Boolean isITrelevant;
private String descriptionProcess;
private int classCalculated;
private LocalDateTime createdDateAndTime;
private Questionnaire questionnaire;//separate model class
private Process process;//separate model class
private Set<Answer> answers = new HashSet<>();// Set of separate model class
}
Answer.java
public class Answer {
private UUID id;
private Question question;// separate class
private PossibleAnswer selectedPossibleAnswer;// separate class
private String answerComment;
private AnsweredQuestionnaire answeredQuestionnaire;
}
The controller handler to display the questionnaire page is as shown below.
#GetMapping("/review/process/{id}")
public String reviewProcess(#PathVariable UUID id, Model model){
Process byId = processService.findById(id);
if(byId != null){
if(byId.getAnsweredQuestionnaires().size() > 0){
Set<AnsweredQuestionnaire> answeredQuestionnaires = byId.getAnsweredQuestionnaires();
AnsweredQuestionnaire lastUpdatedAnsweredQuestionnaire = answeredQuestionnaires.stream().sorted(Comparator.comparing(AnsweredQuestionnaire::getCreatedDateAndTime).reversed())
.collect(Collectors.toList()).get(0);
System.out.println("AnsweredQuestionnaire for a given process :"+lastUpdatedAnsweredQuestionnaire);
model.addAttribute("answeredQuestionnaire", lastUpdatedAnsweredQuestionnaire);
model.addAttribute("isIT_SiG_relevant", lastUpdatedAnsweredQuestionnaire.getIsIT_SiG_relevant());
model.addAttribute("isRiDARrelevant", lastUpdatedAnsweredQuestionnaire.getIsRiDARrelevant());
model.addAttribute("possibleAnswersIds", getAllSelectedPossibleAnswersWithId(lastUpdatedAnsweredQuestionnaire.getAnswers()) );
}else{
System.out.println("Process without answeredQuestionnaire");
Questionnaire byIsActive = questionnaireService.findByIsActive(true);
AnsweredQuestionnaire emptyAnsweredQuestionnaire = new AnsweredQuestionnaire();
emptyAnsweredQuestionnaire.addQuestionnaireForAnsweredQuestionnaire(byIsActive);
System.out.println("Questionnaire found"+byIsActive);
model.addAttribute("answeredQuestionnaire", emptyAnsweredQuestionnaire);
model.addAttribute("isIT_relevant", false);
model.addAttribute("isRi_relevant", false);
// model.addAttribute("questionnaire", byIsActive);
model.addAttribute("possibleAnswersIds", getAllSelectedPossibleAnswersWithId(emptyAnsweredQuestionnaire.getAnswers()) );
}
}
review.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Review page</title>
</head>
<body>
<h1>This is the review page</h1>
<form action="#" th:action="#{/review_questionnaire}" th:object="${answeredQuestionnaire}" method="post">
<label>
isRirelevant :
</label>
<select th:field="*{isRirelevant}">
<option th:value="false" th:text="no" th:selected="${false==isRirelevant}"></option>
<option th:value="true" th:text="yes" th:selected="${true==isRirelevant}"></option>
</select>
<br>
<select th:field="*{isITrelevant}">
<option th:value="false" th:text="no" th:selected="${false==isITrelevant}"></option>
<option th:value="true" th:text="yes" th:selected="${true==isITrelevant}"></option>
</select>
<table>
<thead>
<tr>
<th>Question</th>
<th>Answer</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<tr th:each="question : ${answeredQuestionnaire.questionnaire.getQuestions()}">
<td th:text="${question.text}" th:field="*{#lists.toList(answers)}">Question</td>
<td>
<select>
<option value="0" >Select Answer</option>
<option th:each="possibleAnswer : ${question.getPossibleAnswers()}" th:value="${possibleAnswer.id}" th:text="${possibleAnswer.text}" ></option>
</select>
</td>
</tr>
</tbody>
</table>
<br>
<input type="submit" />
<br>
<label >Class calculated : </label>
<input type="number" th:field="*{classCalculated}" th:value="${answeredQuestionnaire.classCalculated}" readonly >
</form>
</body>
</html>
Update
I separated the answers (Set) with th:if conditional, but still I cannot submit the form.
<div th:if="${answeredQuestionnaire.answers.size()>0}">
<table >
<thead>
<tr>
<th>Question</th>
<th>Answer</th>
<th>Comment</th>
</tr>
</thead>
<tbody th:field="*{answers}" >
<tr th:each="answer,iStat : ${answeredQuestionnaire.answers}" >
<td>
<input th:text="${answer.question.text}" th:value="${answer.question.id}" th:field="*{answers[__${iStat.index}__].question.id}"/>
</td>
<td>
<select th:field="*{answers[__${iStat.index}__].selectedPossibleAnswer.id}">
<option th:value="0" >Select Answer</option>
<option th:each="possibleAnswer : ${answer.question.getPossibleAnswers()}" th:value="${possibleAnswer.id}" th:text="${possibleAnswer.text}" th:selected="${possibleAnswer.id==answer.getSelectedPossibleAnswer().id}" ></option>
</select>
</td>
<td>
<input th:text="${answer.getAnswerComment()}" th:field="*{answers[__${iStat.index}__].answerComment}" >
</td>
</tr>
</tbody>
</table>
</div>
Some suggestions :
1/ You should use Command objects or DTO and not Entity from your model directly to bind datas from your web forms.
2/ You should use a List to bind datamodel relations, it works with Thymeleaf but Set will not work.
3/ You can manage relations with DTO which are containing subsets of your model objects, and simplify the business process a lot and the wireload to your services and database(s). It depends how your model are related too btw.
4/ Disable OpenSessionInView and make a service layer with Transactional annotated methods
5/ Never use an Entity inside a Controller. It's an architectural conception's nonsense.

Can I use this Form in symfony 4?

I have added this form in twig, i need to know if this is okay and how can i recupere the input name="comments" in controller
<form action="{{ path('Update') }}" method="POST">
<input class="form-control" type="text" name="comments"
value=""></td>
<td>
<input type="submit" value="Save"/>
</form>
You can take a look at the Symfony Request Object section of the doc:
// retrieves $_GET and $_POST variables respectively
$request->query->get('id');
$request->request->get('category', 'default category');
So you can retrieve in the controller as:
$request->request->get('comments');
In your controller you can use the Request object to get all the parameters of your form, for example:
/**
* #Route("/Update")
*/
public function update(Request $request){
$comments = $request->request->get('comments');
...
}
But I recommend you to use the forms component.

ErrorException (E_ERROR) Undefined variable: programs

I have searched multiple questions similar to my question but it still doesn't solve it.
Here is my web.php (route)
Route::get('/page/dpage1','ApplyProgramController#index')->name('dpage1');
Route::get('/page/dpage1', 'ApplyProgramController#create');
Route::post('/page/dpage1','ApplyProgramController#store');
My blade.php
<tr>
<strong>
<th>Kod Program</th>
<th>Nama Program</th>
<th>Jawatan</th>
<th>Mohon</th>
</strong>
</tr>
#foreach($programs as $program)
<tr>
<td><strong> Kod Program : {{ $program->kod_program}}</strong></td>
<td>
<input type="text" name="kod_program" readonly value="{{ $program->kod_program}}" class="form-control"></td>
<td><strong> Nama Program :</strong></td>
<td>
<input type="text" name="nama_program" readonly value="{{ $program->nama_program}}" class="form-control"></td>
</tr>
<td>
The error is at the row #foreach($programs as $program).
Here is my controller php
class ApplyProgramController extends Controller
{
public function index()
{
$programs = Program::all();
dd($programs);
return view('page.dropdown1',compact('programs');
I tried several methods such as changing compact to ['programs'=>$program]. When I tried using ifisset($programs), I am able to view the blade page but there is no data shown on the page from my database. The code earlier can be used from the other pages.

Why function relates to code that is not in function?

I have the following controller:
<?php
namespace Dev\TaskBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
use Dev\TaskBundle\Entity\User;
class UserController extends Controller
{
/**
* Redirects to directory where you can input new password
*
* #Route("/{id}", name="user_update")
*/
public function userUpdatePanel($id)
{
$user = $this->getDoctrine()->getRepository('DevTaskBundle:User')->find($id);
return $this->render('DevTaskBundle:User:userUpdate.html.twig', array('user' => $user));
}
/**
* Updates password of the user
*
* #Route("/updatePassword", name="updatePass_action")
*/
public function passUpdateAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$id = $request->get('id');
$user = new User();
$user = $em->getRepository('DevTaskBundle:User')->find($id);
$password = $request->get('passInput');
$user->setPassword($password);
$em->flush();
$users = $this->getDoctrine()->getRepository('DevTaskBundle:User')->findAll();
return $this->render('DevTaskBundle:User:accounts.html.twig', array('users' => $users));
}
}
Here is form from userUpdate.html.twig
<form action="{{ path('updatePass_action') }}" method="POST">
<div>
Username: {{ user.username }}
</div>
<input type="hidden" name="id" value="{{user.id}}">
<div class="form-group">
<label for="passInput">Password:</label>
<input type="text" class="form-control" id="passInput" name="passInput" value="{{ user.password }}">
</div>
<button type="submit" class="btn btn-default">Change</button>
</form>
and when I proceed to change the password I receive such error
Impossible to access an attribute ("username") on a null variable in DevTaskBundle:User:userUpdate.html.twig at line 23
After changing the password I want to render accounts.html.twig so why there is the error about userUpdate? What is wrong here?
Part of accounts.html.twig with twig scripts:
<table class="table table-bordered">
<thead>
<tr>
<th>Login</th>
<th>Hasło</th>
<th>Narzędzia</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>
{{ user.username }}
</td>
<td>
{{ user.password }}
</td>
<td>
<a href="{{ path('user_update', {'id': user.id}) }}" >
<span class="glyphicon glyphicon-pencil linkDeco" ></span>
</a>
{% if user.status == 1 %}
<a href="{{ path('deleteAction', {'name': 'User' ,'direction': 'account_panel' , 'id': user.id}) }}" >
<span class="glyphicon glyphicon-trash linkDeco"></span>
</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
EDIT
problem must be related to userUpdate.html.twig because when I deleted
<div>
Username: {{ user.username }}
</div>
I have the error
Impossible to access an attribute ("id") on a null variable in DevTaskBundle:User:userUpdate.html.twig at line 24
Any idea why is that?
Doctrine find method return array of objects, use findOne instead of find when you expecting only one result.
$user = $this->getDoctrine()->getRepository('DevTaskBundle:User')->findOneById($id);

Thymeleaf-Spring BigDecimal formatting with #NumberFormat adds extra symbols

I'm using Thymeleaf 2.1.2.RELEASE with Spring MVC 4.0.4.RELEASE
I have a dynamic form that I use to add new order lines to an order.
The problem I am facing is that each time I add a line and the content is re-rendered then an extra symbol is added before the currency symbol on the price column of each of the previous lines.
So if I add three rows i get
£22.00
£22.00
£22.00
The price field is BigDecimal with #NumberFormat(style = NumberFormat.Style.CURRENCY) so Spring should handle the conversion.
<div>
<label th:text="#{order.lines}">Order Lines</label>
<table id="addTable" dt:table="true" dt:sort="false" dt:paginate="false" dt:info="false" dt:lengthchange="false">
<thead>
<tr>
<th th:text="#{order.lines.head.linenum}">line</th>
<th th:text="#{order.lines.head.product}">Product</th>
<th th:text="#{order.lines.head.description}">Description</th>
<th th:text="#{order.lines.head.quantity}">Quantity</th>
<th th:text="#{order.lines.head.price}">Price</th>
<th>
<button type="submit" name="addLine" th:text="#{order.line.add}">Add line</button>
</th>
</tr>
</thead>
<tbody>
<tr th:each="line,lineStat : *{lines}">
<td th:text="${lineStat.count}">1</td>
<td>
<input type="text" th:field="*{lines[__${lineStat.index}__].productIdentifier}"
th:errorclass="fieldError"/>
</td>
<td>
<input type="text" th:field="*{lines[__${lineStat.index}__].description}"
th:errorclass="fieldError"/>
</td>
<td>
<input type="text" th:field="*{lines[__${lineStat.index}__].quantity}"
th:errorclass="fieldError"/>
</td>
<td>
<input type="text" th:field="*{{lines[__${lineStat.index}__].price}}"
th:errorclass="fieldError"/>
</td>
<td>
<button type="submit" name="removeLine" th:value="${lineStat.index}"
th:text="#{order.line.remove}">Remove line
</button>
</td>
</tr>
</tbody>
</table>
</div>
This is then backed by class with
public class OrderLine implements Serializable {
#NotEmpty
private String description;
#NotNull
#NumberFormat(style = NumberFormat.Style.CURRENCY)
private BigDecimal price;
#NotEmpty
private String productIdentifier;
#NotNull
#Min(value = 1)
private Integer quantity;
and then in my controller
#RequestMapping(value="/customer/orders", params={"addLine"})
public String addLine(final Order order, final BindingResult bindingResult) {
order.getLines().add(new OrderLine());
return "customer/orders";
}
The html page aleady includes
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
and the character encoding servlet filter is set up as below
#Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return new Filter[] {characterEncodingFilter};
}
Further to this from using Fiddler I can see that the response header to the dandelion datatables ajax request is incorrectly encoded as ISO-88591. I'm using datatables-thymeleaf 0.93 with datatables 1.9.4
From experimenting if I set thymeleaf encoding, the spring servlet filter and the html meta tag to ISO-88591 then the currency symbol appears correctly rendered although I would like this to work with UTF-8
Eventually I found an answer in this post CharacterEncodingFilter don't work together with Spring Security 3.2.0 provided by #Christian Nilsson. Basically I needed to force the character encoding filter to be registered using the onStartup method rather than the usual getServletFilters.

Resources