Laravel Views Subfolders routing - php

I have sub folders inside my view folder called
--views
---saledata(folder)
----saled.blade.php
---uploader(folder)
----datauploader.php
---home.blade.php
I want to route this saled.blade.php and datauploader.php files in web.php
I have tried with
route:: view('saledata','../saledata/saled');
route:: view('datauploader','datauploader');
in both the ways.but both shows 404 errors.
and this is the way I mentioned the url of the file in home.blade.php
<a href="{{ url('saledata') }}" title="">
So please help to resolve this

if directly form the web file :
Route::get('saledata', function () {
return view('saledata.saled');
});
if want to use in the controller:
public function index(Request $request) {
return view('saledata.saled');
}

//this code will be the laravel v8
//Route File code
Route::get('saledata',[yourControllerFile::class,'your_controller_method']);
//Controller file code
class yourControllerName extends controllers {
public function your_controller_method() {
return view('saledata');
}
}

In route file:
Route::view('/saledata', 'saledata.saled')->name('saledata');
And in url:
<a href="{{ route('saledata') }}" title="">
Hope it will works.

Related

Laravel Blade return output of a "route name", not the URI

I have this footer inside a blade template. This footer should be displaying a variable dynamically inside the said template.
template.blade.php
<html>
<body>
#include('templates.usermenu.usermenu')
#include("templates.navigation.navigation")
#yield('header')
#yield('content')
{{ route('footer-links') }} <--this is the footer. this only outputs the "URI"
</body>
</html>
here is the output of the code above
Here is the footer.blade.php with a variable that should be displayed:
footer.blade.php
{{-- buttons --}}
#foreach ($footerLinksRecord as $record)
<div class="clearfix">
<a class="poppins-medium text-md button-white text-left float-left clickable" href="{{ $record->link }}">{{ $record->name }}
</a>
</div>
#endforeach
The footer.blade.php needs to output a variable coming from its FooterLinksController#index (the template.blade.php does not have any controller managing it).
Here is the route for the footer.blade.php and the home.index (home.index calls the template.blade.php inside its code):
Route::get('/', function () {
return view('home.index');
})->name('home');
// Footer Links Index
Route::get('/footer', [FooterLinksController::class, 'index'])->name('footer-links');
Here is the FooterLinksController#index:
public function index()
{
$footerLinksRecord = FooterLinks::all();
return $footerLinksRecord;
}
What should happen is that the footer.blade.php should output the data of its route when called, not the URI.
How should this be approached?
edit removed #include. I'm looking at the wrong code.
You could use a View Composer to handle the data for the footer view. You can include the footer where needed without worrying about the data needed for it.
You can create a new Service Provider or add to the boot method of an existing one:
public function boot()
{
...
View::composer('footer', FooterComposer::class);
}
You can use a class with a compose method to handle composing the view instead of a Controller:
class FooterComposer
{
...
public function compose($view)
{
$view->with('footerLinksRecord', FooterLinks::all());
}
}
In the layout, template.blade.php, you only have to 'include' the footer:
#include('footer')
You also have the option of making a Blade component for the footer instead.
Laravel 8.x Docs - Views - View Composers
Laravel 8.x Docs - Blade - Components

Laravel use a controller in blade

I just start to learning Laravel, I stuck in a part, I want to get data from database and pass it to a blade (view) file then use this view in another view file, and want to pass one variable to a controller but I got this error:
"Class 'adsController' not found"
web.php
Route::get('seller/panel', 'sellerController#panel');
panel.blade.php
#include('seller\hook\ads')
sellerController.php
public function panel(){
$ads = DB::table('ads')->get();
return view('hook\ads', ['ads' => $ads]);
}
adsController.php
class adsController extends Controller {
public function getStatus($p) {
if ($p == 'something') {
$status = 'yeah';
} else {
$status = 'blah blahe';
}
return view('hook\ads', $status);
}
}
ads.blade.php
<div class="ads">
#foreach ($ads as $ad)
{{ $ad->name }}
{{ adsController::getStatus($ad->approved) }}
#endforeach
</div>
So, as you see I am tring to get data from database in sellerController.php then pass it to ads.blade.php then I want to use adsController.php 's function in ads.blade.php, but it can't find adsController
Sorry I am newbie to laravel
As everyone said, it's not recommended to call the controller from the view.
Here's what I would do :
In your model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Ad extends Model{
public function getStatus()
{
return $this->approved == 'something' ? 'yeah' : 'blah blaehe';
}
}
In your view :
<div class="ads">
#foreach ($ads as $ad)
{{ $ad->name }}
#include('hook.ads', ['status' => $ad->getStatus()])
#endforeach
</div>
In your controller :
public function panel(){
$ads = \App\Ad::all();
return view('hook\ads', ['ads' => $ads]);
}
At the beginning of the blade file you could include the following
#php
use App\Http\Controllers\adsController as adsController;
#endphp
then on your blade template you could use the controller as you have used here.
But it is a really bad habit to use one directly.
Since you are a newbie to Laravel change that coding practice.
You could use a service provider of a sort if you want that data which needs to be shown on a particular blade view all the time.
You should try this despite I don't recommend the approach you are trying to achieve.
{!! app('App\Http\Controllers\adsController')->getStatus($ad->approved) !!}
It should work but that's very wrong.
Most often when you get this error when your namespace declaration in the controller is wrong. Typical scenario is where you generate controller stub with Artisan and then move it somewhere else from the default location.
In some cases you need to run:
composer dumpautoload
To generate new optimized class loader map.

Trying to get property of non-object (show.blade.php)

I have this problem in my routes where in I get this error tying to get property of non-object view (show.blade.php) on clicking a link to view/redirect allstats.blade.php. It is very weird because I am not returning my routes to show.blade.php but to allstats.blade.php. I was wondering if I am doing something wrong with the routes. This is very tricky, I don't know what's causing this error.
#Controller:
EmployeeController.php
public function postdisplaySummaryReport()
{
$employeesquery = Employee::paginate(10);
return View::make('employees.allstats')
->with('employeequerytoallstat', $employeesquery);
}
#Routes:
routes.php
Route::post('employees/sumarryReports','EmployeeController#postdisplaySummaryReport');
Route::resource('employees', 'EmployeeController');
#index.php(URL to the allstat.blade.php):
<span class="glyphicon glyphicon-folder-open"></span> {{trans('labels.payroll_reports.lbl_summary_report')}}
#ERROR:
Just change your function to this.
public function postdisplaySummaryReport()
{
$employeesquery = Employee::paginate(10);
return view('employees.allstats')
->with('employeequerytoallstat', $employeesquery);
}
also change your Route method to get instead of post if you want to display content.
Route::get('employees/sumarryReports','EmployeeController#postdisplaySummaryReport');

Laravel empty page with no error calling route's method

I have a methods that return base64 data image to a view after making a GET request to www.website.com/preview/{id}.
It is called by an <a> tag inside view.blade.php:
<a class="image-popup-vertical-fit" href="{{url(Config::get("app.previewPath") , $encrypted)}}" >
<img class="issue_img" src="{{App\Http\Classes\RepositoryUtil::getSmallImage($encrypted)}}" alt="{{ $name }}">
</a>
It work well if I declare a GET route with the code function inside routes.php:
Route::get(Config::get("app.previewPath") . "/{id}", function(\Request $request, $encrypted){
// ... some code ...
$base64 = \App\Http\Classes\RepositoryUtil::retriveImage($encrypted);
#readfile($base64);
});
But if I move the same code inside a controller's method, it return a blank page!
Route::get(Config::get("app.previewPath") . "/{id}", "MyController#getPreview");
MyController.php
public static function getPreview(\Request $request, $encrypted){
// ... same code as routes.php ...
$base64 = \App\Http\Classes\RepositoryUtil::retriveImage($encrypted);
#readfile($base64);
}
Where am I wrong?
I figure it out, it was a "distraction error".
I leave a middleware enabled in all methods of the controller, and it fails printing dd("Not authorized!"); without return before it.
It didn't return nothing without any errors!
I restrincted the middleware only to selected actions.
Thanks for support.

In template How to print a link from a route name

I would like display a link using a route name (home) into my template.
How can I do with Slim Framework
Thanks
My route
<?php
// index : home
$app->get('/home', function () use ($app){
$app->render('home.php');
})->name('home');
My template
<div>
my home link
</div>
or with urlFor()
<div>
my home link
</div>
I got this message
=> Call to undefined method Slim\View::urlFor()
The Slim instance is accessible through a Singleton and its getInstance method
my home link
You can also specify a name if you have multiple instances of Slim
my home link
If you want to access the urlFor method using $this
my home link
Then you should create a Custom View by adding a subclass of Slim\View containing a urlFor method and link it to Slim
Custom class :
<?php
class CustomView extends \Slim\View
{
public function urlFor($name, $params = array(), $appName = 'default')
{
return Slim::getInstance($appName)->urlFor($name, $params);
}
}
Linking :
<?php
require 'CustomView.php';
$app = new \Slim\Slim(array(
'view' => new CustomView()
));
I found the solution
just add this
$app->hook('slim.before.router', function () use ($app) {
// Pass in the App so we can use urlFor() to generate routes
$app->view()->setData('app', $app);
});
An then into your template you can use this (with app not this):
<div>
my home link
</div>
OR:
echo 'home';

Categories