suppose I want to go a new link like www.facebook.com. How do I do this in laravel 4. If i use return Redirect::to() then I get the error that
there is no method called www.facebook.com
in my controller. If I use header('Location: ' . $url); then I get the error
Undefined variable: content
since there is a code in my controller which says protected $layout = 'sites.master' in in the layouts/master file there is a {{$content}}
You can use the URL::route('named_route')') function inside the href attribute in the blade syntax to go to a particular named route and URL::to('link_adress') function inside the href attribute in the blade syntax to go a particular custom address.
Example code for creating a link to google would be like:
<a class="link" href="{{ URL::to('http://www.google.com') }}">Go to google</a>
The view should use the blade templating engine for this to work.
Related
I have route file defined as follows...
Route::get('pincodes/export', 'PincodeController#export'); Route::resource('pincodes','PincodeController');
I have a controller called 'PincodeController' where the default functions works without any issue. I wanted to add a new function called 'export' in this controller. I tried {{ route('pincodes.export') }} in my blade file to generate link to this page... and it throws the following error...
Facade\Ignition\Exceptions\ViewException Route [pincodes.export] not defined. (View: /Users/tst/Desktop/www/laravel/project/resources/views/pincode/index.blade.php)
but if i access the url directly in browser it works fine. why is that ?
You need to specifically name the route:
Route::get('pincodes/export', 'PincodeController#export')->name('pincodes.export');
If you are using Laravel 5.5+, change export route like this
Route::get('pincodes/export', 'PincodeController#export')->name('export');
use route function in your blade file like
route('export')
I can't find solution to my Laravel routing error. Whenever I visit the link it shows the error 404 - page not found. Can any body help on this error. Below is my codes-
In web.php
Route::get('/post-ads-new/{cat}/{sub_cat}','PostController#postAdsNew');
In PostController.php
public function postAdsNew($cat,$sub_cat)
{
return view('post-ads-new');
}
And used the url in HTML as
<a href="<?php echo $url.'/post-ads-new/?cat='.$category->id;?>&<?php echo 'sub_cat='.$sub_category->subid;?>">
You should use Laravel URL generator by adding a name to your route
Route::get('/post-ads-new/{cat}/{sub_cat}','PostController#postAdsNew')->name('ads');
And in your view:
Blade version:
Native PHP version:
Here is the documentation https://laravel.com/docs/7.x/urls
The variables are unnecessary in the url, you will need to construct your url like the following:
I want to pass a object from the blade file to the controller file. The purpose is when the user click an edit button the user will get a form which is filled with the previous input data. I am using this code in the blade file:
Edit
But When I want to get the passed object from the controller's edit method I get a null. My Controller code is like this now:
public function edit(FeesType $feesType)
{
//
dump($feesType->name);
return view('feestype.edit',['feesType'=>$feesType]);
}
Here I have dump the $feesType object but I get a null. Please help me how can I solve this problem.
Thanks in advance
Route model binding works a bit different here is the documentation
What you need to do is have your route like this:
Route::get('feestype/{feesType}/edit', 'YourController#edit')->name('feestype.edit');
then in your view
Edit
-- EDIT
using a resource file:
Route::resource('feestype', 'YourController')
the link will be built the same as above:
{{ route('feestype.edit', $feesType) }}
you should change your Route to :
Route::put('feestype/{id}/edit', 'YourController#edit');
For update and edit you should use put not get.
Note that for this code:
Edit
first you should compact $feestype in YourController then use your code in blade.
Now the code in the blade file is
Edit
The controller file contains this code:
public function edit(FeesType $feesType)
{
//
$feesType = FeesType::find($feesType->id);
dump($feesType->name);
return view('feestype.edit',['feesType'=>$feesType]);
}
And here is my Route definition:
Route::resource('feestype','FeesTypesController');
And the browser shows this message:
Hi I have a hyperlink from a page:
<h3>hitest</h3>
the route:
Route::get('hitest', function(){ return 'hitest message';});
There is an error:
No query results for model [App\User2].
hyper link is from a page with this url
/userpage/1
the 1 is a model object.
Shouldn't the hyperlink route to /hitest ?
Please see my other post: Strange behavior with routing and hypertext.
I'm new at web development. Is there configurations for routing? The app is hosted (not local).
As bytesarelife already mentioned, you can use the url()-function, like so:
{{ url('your/url/') }}
A better way in terms of maintainability would be to give your routes names, so you would not have to replace every url in every template once you want to change it. You can do so, by adding the name in your routes:
Route::get('hitest', function(){ return 'hitest message';})->name('getHittest');
And then you can use the route function in your view:
{{ route('getHittest') }}
Here is my controller code:
$view = View::make('homepage');
$loglink = URL::route('account.login');
$view->loglink = $loglink;
return $view;
Here is how I recv it in view:
<a href="{{ $loglink }}" class="red">
I get an error saying:
Undefined variable: loglink (View:
C:\BottleBookings\server\app\views\partials\header.blade.php)
Any problem with the way I am sending or recving it?
Thats not how view creation works. Just return your view and pass the variables you want to use with the with() function in the same command. Create your view like this:
$loglink = URL::route('account.login');
return View::make('homepage')->with('loglink', $loglink);
Or even shorter:
return View::make('homepage')->with('loglink', URL::route('account.login'));
Also, you don't really have to pass the link URLs, it's perfectly fine to generate them where they are needed in the view:
<a href="{{ URL::route('account.login') }}" class="red">
why you complicate. just put the datas in array and pass it to view.
Controller
$data['loglink'] = URL::route('account.login');
return View::make('homepage',$data);
p.s. your loglink variable will be available to only homepage view. NOT on header page.
if you want to pass the loglink to header, then you have to pass it as View::composer or View::share or where you are including the file. any one of the three will do.