I'm new to Laravel 5.4 and php and I'm having some trouble to do my routing, so I wish you would explain to me what's going on !
I'm developing a back office for my future projects, and for my current project I need to be able to update data sent to the homepage. The edit of the page and the update of the data is now fully functional, I'm just having some trouble to do the link in the layout. Pretty sure it's a trivial problem, yet i'm having trouble figuring it out.
Here is my code :
web.php :
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
Route::resource('homepage', 'Admin\HomeController');
});
HomeController :`
public function edit($id) {
$contentExists = Homepage::first();
$homepage = Homepage::findOrFail($id);
return view('admin/homepage/edit', compact('homepage', 'contentExists'));
}`
In the layout :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
Now for that I know I need to pass a parameter to the route, so I did like so :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
But then it says the homepage variable is not defined, and I have no idea where I can define this variable since I'm in the layout, and also have no idea what needs to be inside this variable, what's his purpose... Can you help me with that ?
route second parameter, must be an array like this for example:
route('homepage.edit', ['id' => 2])
Related
I've come across a peculiar bug in a laravel project of mine,
In my web.php I have a named route 'welcome' :
Route::get('/', function () {
return view('welcome');
})->name('welcome');
This route contain my index page in which there's a navigation component.
Through an AppServiceProvider I'm passing data to the navigation component
The data im passing is a simple table containing this :
{
name : "home",
href : "route('welcome')"
}
<nav>
#foreach
{{ myData->name }}
#endforeach
</nav>
The problem is that when I click on the link "home" it sends me to this address :
http://127.0.0.1:8000/route('welcome')
https://i.stack.imgur.com/FaET1.png
Instead of http://127.0.0.1:8000/
and this is a problem with every single link, it always returns
http://127.0.0.1:8000/route('myLink')
this is my complete web.php for further information
Route::get('/', [HomeController::class, 'index'])->name('welcome');
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
require __DIR__.'/auth.php';
Route::resource('/nav', NavController::class);
// // Fallback page error 404
Route::fallback(FallbackController::class);
If you need more information please ask me, i've been trying to fix this issue for 2 days, and it is frustrating me.
I tried route:clear, cache:clear, view:clear and nothing changes.
Is it the format at which I've stored the name of the route that isn't working ?
The thing is it was working for an hour or two. and now impossible to fix it.
I hope someone here can help.
Josh's solution is working.
The route('welcome') was passed through as a string. and the function route() was not called.
In your data, for the key href you just need to store welcome instead of route('welcome'). Then in your blade template, you can do
login here
The reason your solution is not working is that you just have the word route stored as a string, you aren't actually calling the function called route.
Title atributes in Laravel, how to do it?
Hello, my question to Laravel masters is:
my /routes/web.php looks something like this:
Route::get('/', function () {
return view('index', []);
})->name('index');
Route::get('/services', function () {
return view('services', []);
})->name('services');
Route::get('/contact', function () {
return view('contact', []);
})->name('contact');
And then in the blade template I want to list the links, they are in the navigation bar that is located in the master layout and I yield the content of the site. Sample:
<ul class="nav-menu align-to-right">
<li>Home</li>
<li>Services</li>
<li>Contact</li>
</ul>
Surely you understand that because of SEO I don't want to mention only the name of the company/webpage, but also the subpage. For example, "MyCompany | Our services".
The problem is that these links can also be, for example, in the footer, somewhere in the text on some page, etc. I have to go everywhere and manually change the wording of the title attribute if necessary. For example, on "MyCompany | Our great service".
Well, and especially when I am in the blade template services.blade.php where I define the title of the page, it looks like this:
#section('title', '| Services')
So I just need this title to match the title attribute in a href anywhere on the site.
Perhaps I have explained and described my problem in order. The project runs on Laravel 9.x
I am new to it (but not in PHP), but I still ask you for a specific explanation, solution, which is more detailed, so that it is easier for me to understand this problem and I can learn from it for the future solutions. Thank you
I am new to Laravel , I am getting 404 not found error when returning view to salary report from my controller. The below mentioned is my function which returns my simple view to salary report.
public function getSalaryReport()
{
return view('Company.salaryReport');
}
the routes.php file ahs route to company controller.
Route::group(['middleware' => 'auth.company'], function () {
Route::get('company/notice-board/create', 'CompanyController#getNoticeBoardCreate');
Route::get('company/notice-board/{id}/edit', 'CompanyController#getNoticeBoardEdit');
Route::get('company/designation/{id}/edit', 'CompanyController#getDesignationEdit');
Route::get('company/all-user/{id}/force', 'CompanyController#getForce');
Route::post('company/all-user/{id}/force', 'CompanyController#postForce');
Route::controller('company', 'CompanyController')
this is my view which i am trying to display from my controller.
#extends('Company.CompanyLayout')
#section('content')
<div>
<ul class="breadcrumb">
<li>
Home <span class="divider">/</span>
</li>
<li>
<a href='{!! URL::to("company/report-summery") !!}'>Summery Report</a>
</li>
</ul>
</div>
#endsection
where i am going wrong and what should be done to make my view visible. Thanks to all in advance.
Route::controller is depricated in the latest versions of Laravel, try not to use it anymore.
You can use Route::resource or create a specific route for your salary report like this:
Route::get('company/salary-report', 'CompanyController#getSalaryReport');
Also make sure that you have resources\views\Company\salaryReport.blade.php as your view.
404 not found is an error because you don't have any routes for the given url. And I didn't find any routes in your example for the function getSalaryReport()
if you want to call this method, at least add this to your routes:
Route::get('company/report-summery', 'CompanyController#getSalaryReport');
Blade not working (Laravel), why is nothing shown?
1.show.blade.php
#extends('book.show')
#section('comment')
Yuup!
#endsection
2.book/show.blade.php
<ul class="cols">
<li>
#yield('comment')
</li>
</ul>
Nothing was wrong with the displayed code. The problem was in your routes. Here is a snippet from your routes:
Route::get('book/{book}', function () {
$comment = 'Hello';
return view('comment.show', ['comment' => $comment]);
});
Route::resource('book', 'BookController');
Route::resource('book') creates the exact same URI as 'book/{book}' so it overrides the first one. In other words, your closure is never triggered. You have several options.
Don't use Route::resource. I like to be explicit with my routes.
Put your Route::get('book/{book} after the Route::resource. This would work too.
Remove Route::get('book/{book}') and just add your code inside your BookController classes show method.
Any of these 3 options will work. I suggest option #3 if you like using Route::resource. Otherwise, I would work with option #1. Option #2 and overriding other routes and such isn't a very nice way of going about things in my opinion.
According to your code in pastebin.
Change return view('comment.show', ['comment' => $comment]); to return view('book.show', ['comment' => $comment]);
EDIT
Another problem is you end your section with #endsection. It will be #stop.
Your code should look like this:
#extends('book.show')
#section('comment')
Yuup!
#stop
I have a basic application being baked by CakePHP.
Now i want to add a new action'listjobs' in my model controller class and a corresponding 'listjobs' view in the /Template/Job/listjobs.ctp.
From my model's index view i added one more action in the side navigation bar.
like this
<li><?= $this->Html->link(__('View Jobs'),['action' => 'listjobs']) ?></li>
When i click on the link 'View Jobs' control is directed to the action method of my model's controller but its not taking the list jobs 's view.
Code for my action method
public function listjobs()
{
$this->log("inside list jobs",'debug');
$this->render('listjobs');
}
The listjobs.ctp contains a very basic code as following
<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
<ul class="side-nav">
<li>< Git Hub >
</li>
<li>< Stack overflow >
</li>
</ul>
</div>
Not able to find out why the view is not rendered rather the index view is getting rendered.
Links loaded according to chrome console in order
(1) http://localhost/myjobs/job/listjobs
(2) http://localhost/myjobs/job
So (1) should have been loaded with the view..it should not been redirected to (2)
Regards,
Saurav
I think you need to use like this.
<li><?php echo $this->Html->link('View Jobs',array('controller' => 'MyModel', 'action' => 'listjobs')); ?></li>
Your view will be.
public function listjobs()
{
$this->log("inside list jobs",'debug');
}
I think you don't need to render, your view will be render by default, you will need to send data like that $this->set(compact('VarName'));
Secondly $this->log("inside list jobs",'debug'); if log is a method then you need to set action, then it will be.
$this->setAction('log');
Try, if you get error, then let me know. If your work proper then, you view will work.
http://localhost/myjobs/job/listjobs
this link defines.
http://localhost/SiteName/ControllerName/ActionName/Parameter
Get review and study this page.
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html
Thanks
ok...i did some debugging of the cakephp framework code.
I was using an auth component for my login functionality.
I found that the newly added action was not allowed to pass the through filter of auth component. so i added the newly added action in the beforefilter method of appcontroller like this. i guess the crud operations are already added in the filter by default.
$this->Auth->allow(['index', 'view', 'display','listjobs','add','delete']);
cheers,
Saurav