I am trying to load a view from my controller using the follow code but I only get a raw HTML view and does not show the site's template.
$view = $this->getView( 'download', 'html' );
$view->display();
Can some help me in what I am doing wrong to display the site's template.
I also tried a redirect but that did not work either
$this->redirect(JRoute::_('index.php?option=com_atdwcsv&view=download'), false);
Edit: I figured out what was wrong with the redirect. Code I needed was
$this->setRedirect('index.php?option=com_atdwcsv&view=download');
$this->redirect();
I could be wrong, but I don't think you need to use the display() method on the view, I think you need to use $this->display(); instead.
Related
I have a view mypage.blade.php and a route.
The url is like : https://example.com/mypage/param1/param2. The route use param1 and param2 and generate the page.
Question 1
In that page, I try to get its HTML code. Is there a way to do it?. I tried render() but I don't get what I want.
Question 2
In the view, can I get the HTML code of an other view by specifying a path ?
You had the right idea. Not sure why it wouldn't work for you.
In the controller, set the view into a variable:
$view = view('myBaseView', compact('people', 'places', 'things'));
Now, if you dump the rendered view variable, you have the page's HTML:
dd($view->render());
To get the html of another view by specifying the path and using the internal controller, you would need to set up some kind of a wrapper or catch so that the view variable is not returned as a view, but rendered out to html as above. Your method would need to trap whatever the original controller was sending before it pushed out the view.
Of course, old school php can get the other page's rendered html too possibly if your server is set to allow this:
$html = file_get_contents('http://mypage.com/');
Something else you might find handy is the Laravel sections method. If you just want to render part of the page you can do so by calling whatever section you want from a partial view:
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
dd($sections['modalContent']); // this will only dump whats in the content section
I don't know what you want to do with this html, but if you wish to display it on a page, once you send it (you'd possibly want to return the view along with a compact of the variable $view... as a normal variable if so), remember to use this format:
{!! $view !!}
HTH
Currently my users must get the visit form given by Route::get then fill it in to get back a result view given by Route::post. I need to create a shareable link such as /account/search/vrm/{vrm} where {vrm} is the VRM that is usually filled in on the form page. This VRM then needs to redirected to Route::post as post data. This needs to be done by my controller. How can I do this in my controller?
Routes:
// Shows form view
Route::get('/account/search', 'User\AccountController#getSearch')->name('account.search');
// Shows result view
Route::post('/account/search', 'User\AccountController#runSearch');
// Redirect to /account/search as POST
Route::get('/account/search/vrm/{vrm}', function($vrm) { ???????? });
POSTs cannot be redirected.
Your best bet is to have them land on a page that contains a form with <input type="hidden"> fields and some JavaScript that immediately re-submits it to the desired destination.
You can redirect to a controller action or call the controller directly, see the answer here:
In summary, setting the request method in the controller, or calling a controller's action.
Ps: I don't want to repeat the same thing.
For those who comes later:
If you are using blade templating engine for the views, you can add '#csrf' blade directive after the form starting tag to prevent this. This is done by laravel to prevent cross site reference attacks. By adding this directive, you can get around this.
return redirect()->route('YOUR_ROUTE',['PARAM'=>'VARIABLE'])
This is an extension from a post I made a few days ago: Change a page without refreshing - Laravel / Ajax
Basically, I'm trying to replicate the URL Structure of Soundcloud where you can click on a link, it'll load the content without refreshing the page however if you land directly on that page, it won't replicate design and effecitvely break.
I've been thinking of ways on how I can check in Laravel if the page is requested via Ajax or has been landed on without an Ajax call.
What's happening at the moment is that when I call the page, the view has a master template that's extended thus creating duplicate master templates on the one view.
I was thinking if I done something like
#if(!Request::ajax())
#extends('masterlayout')
#endif
It would work but tried and no luck.
Any help as always is greatly appreciated!
Thanks
Looks like #extends directive is always executed even in falsy if
You can add an empty layout and perform check like this:
#extends(Request::ajax()? 'layouts.empty' : 'layouts.master')
and you need add only this in layout empty.blade.php:
#yield('content')
or you can add ajax check in the master layout
#if(!Request::ajax())
// all layout code
#else
#yield('content')
#endif
You can check that in your controller action.
public function index(Request $request)
{
if($request->ajax()){
return "This is an AJAX call!";
}
return "Not an AJAX call...";
}
Thanks for all the answers, I eventually got this fixed by using #if(!Request::ajax()) on section that didn't need to be shown when the page was loaded via ajax.
Thank you again! :D
so im working on a page to learn Yii. this is it: http://devcave.freeiz.com/
What i am trying to do is, when i click on login, a div is sliding down, where the login.php form should be.
The question is how do i render that into the main.php's div tag.
i tryed $this->renderPartial('//site/login',array('model'=>$model)); , but i get Undefined variable: model error.
I read trough the Understanding yii view rendering flow but it seems i didnt quit get the point.
Any suggestions please?
Use this:
$this->renderPartial('site/login',array('model'=>new LoginForm));
And in login view you should define action like this:
$form=$this->beginWidget('CActiveForm', array(
// ...
'action' => $this->createUrl( 'site/login' ),
// ...
The error is that you didn't defined a variable named $model in your controller and this variable is needed in the View login.
In your controller when you call
$this->render('yourView', array());
you'll have to add in the second array the datas that you want to pass to the Login view
$this->render('yourView', array('model'=>$model));
Don't hesitate to post your code if you need a more specific answer!
I'm new to CI & PHP.
I have an auth library included, and works great stand-alone.
I simply want to have the login form load as a view inside another view...is that weird?:
I'm quasi-templating:
index:
$this->load->view('head_content');
$this->load->view('stuff');
$this->load->view('footer');
Inside the stuff view:
<stuff></>
$this->load->view('login_view');
<morestuff></>
I just want the login form to show up on the front page, and then tie into the auth system...
You have to load the login view in the controller, and then pass the data to the stuff view.
In the controller:
$this->load->view('head_content');
// the line below will save the output of the login view to $data['login']
// instead of outputting to the screen
$data['login'] = $this->load->view('login_view', '', TRUE);
$this->load->view('stuff');
$this->load->view('footer');
In the stuff view:
<stuff>
<?php echo $login; ?>
<morestuff>
In the page Views, at the bottom, check out the section Returning views as data
I've just recommended this but I'm going to recommend it again:
http://codeigniter.com/forums/viewthread/77279/
Django-like template inheritance helper for CodeIgniter.
I use this on ALL of my CI projects and it makes things like this stupidly easy.