I made a profile page in Laravel with something like /profile/{name} and I use a UserController#showProfile:
public function showProfile($name)
{
if(!Cache::has($name))
{
if(DB::table('users')->where('name', $name)->exists())
{
$user = DB::table('users')->where('name', $name)->get();
Cache::put($name, $user, 10);
}
else
{
return view ('error404');
}
}
return view ('user.profile', ['profile_stats' => Cache::get($name)]);
}
But nav-tabs don't work if I put /profile/{name}, if I let it just /profile in routes everything works.
This is profile.blade.php with nav-tabs: https://pastebin.com/wzKM8wRd and this https://pastebin.com/XLBKX6Gr is the layout.
Route::get('profile/{name}', 'UserController#showProfile');
can you show us content view script: user.profile.blade.php ?
and don't use cache until you make sure that everything works fine.
public function showProfile($name)
{
$user = DB::table('users')->where('name', $name)->get();
return view ('user.profile', ['profile_stats' => $user]);
}
upd
any js errors in the browser console?
navigation works when url ends with /profile/ ? maybe you should try /profile/{name}/
So fix up js errors... and then come back :) looks like your problem with hash navigation isn't laravel issue.
I solved, problem was:
<script type="text/javascript">
window.jQuery || document.write("<script src='js/jquery.js'>"+"<"+"/script>");
</script>
I forgot to put / in front of js/jquery.js:
<script type="text/javascript">
window.jQuery || document.write("<script src='/js/jquery.js'>"+"<"+"/script>");
</script>
However thanks for your answers!
Related
I have menu links which points to different pages with css animation. My anchor tag is look like this
I create a route for this link but it does not work.
"Route::get('#about',function () {
return view('about');
});
can any one please help me to accomplish this.
Thanks
Because the fragment is processed by the client, the client must be able to render JavaScript before the fragment can be accessed. A solution to accomplish this might be:
In your routes.php file:
Route::get('/', function() {
return view('basic-javascript-router');
});
Route::get('/about', function() {
return view('about');
});
Route::get('/welcome', function() {
return view('welcome');
});
In your project's /resources/views/ directory add a simple basic-javascript-router.php file:
<!DOCTYPE html>
<html>
<head>
<title>rerouting</title>
<script type="text/javascript">
function checkHash() {
if (window.location.hash && window.location.hash === '#about') {
window.location.href = '/about';
} else {
//direct users to the generic landing page
window.location.href = '/welcome';
}
}
window.onload = checkHash;
</script>
</head>
<body>
<p>rerouting...</p>
</body>
</html>
Typically this is a job for a frontend framework like VueJS but this might work for you.
I'm having trouble getting a View Composer to fire based on a subview. Specifcally:
protected $layout = 'layouts.main';
public function index()
{
return $this->layout->content = View::make('pages.dashboard');
}
pages/dashboard.blade.php contains:
#extends('layouts.main')
#section('content')
Hello World
#stop
Inside layouts/main.blade.php contains:
<html>
<body>
#include('layouts.partials.header')
#include('layouts.partials.sidebar')
#include('layouts.partials.content') // Renders {{ $content }}
</body>
</html>
My view controller is:
View::composer('layouts.auth', function($view)
{
die("Hit the sweet spot!");
});
It will work if I use "pages.dashboard", but not "layouts.main" or "layouts.partials.sidebar". Any idea how to hook to those views?
Using the tip at Get location of view passed into laravel view composer for getting a view name, I debugged every view that was called:
View::composer('*', function($view) {
print $view->getName() . "<br>";
});
I found that the proper name to listen for is "layouts.partials.sidebar". Plugged it in and it worked! Guess it was just a typo on my part the first time.
You registered the composer for layouts.auth view
View::composer('layouts.auth', function($view)
{
die("Hit the sweet spot!");
});
So, it'll work only when layouts.auth will be rendered. To hook the composer for layouts.main you should register it for layouts.main using:
View::composer('layouts.main', function($view)
{
die("Hit the sweet spot!");
});
You may also check this article, could be helpful.
IN codeigniter I am repeatedly using the controllers to load all the templates of my page....
I have divided the page into header, top navigation, left navigation and content and footer.
This is what I do at present
public function get_started() {
if (test_login()) {
$this->load->view('includes/header');
$this->load->view('includes/topnav');
$this->load->view('includes/leftbar');
$this->load->view('login_nav/get_started');
$this->load->view('includes/footer');
} else {
$this->load->view('errors/needlogin');
}
}
Is there any jquery-ajax helpers or plugins in codeigniter which would allow me to keep header footer and topnavigation static and allow me to load specific views using ajax.
thanks in advance..
You can use the constructor to set your static header:
//in your controller
public $data;
function __construct()
{
$this->data['header'] = 'static_header';
$this->data['static_footer'] = 'static_footer';
}
function get_started(){
if (test_login()) {
$this->data['subview'] = 'login_nav/get_started';
} else {
$this->data['subview'] = 'errors/needlogin';
}
$this->load->view('template',$this->data);
}
function get_page(){
$view = $this->load->view('your_dynamic_view','',TRUE);
print json_encode(array('success' => TRUE, 'view' => $view);
}
// in your template.php
<div id="header"><?php echo $this->load->view('header');?></div>
<div id="subview"><?php echo $this->load->view('subview');?></div>
<div id="footer"><?php echo $this->load->view('footer');?></div>
// in your script - used to load dynamic view on you subview div
<script type="text/javascript">
$.get('controller/get_page',{},function(data){
if(data.success){
$('#subview').html(data.view);
}
},'json')
</script>
Message me if there's a problem with my code
Happy coding ---> :D
The answer from PinoyPal is theoreticaly correct, but it didn't work for me in practice because it lacks one major detail: a route.
Take a look at this part of their script:
// in your script - used to load dynamic view on you subview div
<script type="text/javascript">
$.get('controller/get_page',{},function(data){
if(data.success){
$('#subview').html(data.view);
}
},'json')
</script>
Here in place of 'controller/get_page' there should be a url for an actual GET request. This is how it is generally supposed to look:
$("a.your_navigation_element_class").on('click',function(e){
e.preventDefault(); //this is to prevent browser from actually following the link
var url = $(this).attr("href");
$.get(url, {}, function(data){
if (data.success){
$('#subview').html(data.view);
}
},'json')
});
Now here's a question: where will this GET request end up? In the default controller route, that's right. This is why you need to 1) modify your request url and 2) set up a route, so that this request will be passed to an ajax-serving controller. Or just add an ajax-serving function to your default controller and re-route ajax requests to it.
Here follows how it should all look wrapped up
In ...\application\controller\Pages.php:
class Pages extends CI_Controller {
...
public function serve_ajax ($page) {
$view = $this->load->view($page, '', TRUE);
print json_encode( array('success' => TRUE, 'view' => $view);
}
...
}
In ...\application\config\routes.php:
...
$route['ajax/(:any)'] = 'pages/serve_ajax/$1';
On your page:
...
<body>
...
<div id="page"></div>
...
<script>
$("a.navigation").on('click',function(e){
e.preventDefault();
var url = $(this).attr("href");
$.get("/ajax" + url, {}, function(data){
//The trailing slash before "ajax" places it directly above
//the site root, like this: http://yourdomain.com/ajax/url
if (data.success){
$('#page').html(data.view);
}
},'json')
});
</script>
</body>
And you're all set.
This code in a simple HTML file works:
<script>
function load() {
alert("load event detected!");
}
window.onload = load;
</script>
However, if I put it into the index.html file of an AngularJS web app, it does not. Does anybody know why not?
Call your function with ng-init
var app = angular.module('app',[]);
app.controller('myController', function($scope){
$scope.load = function () {
alert("load event detected!");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app'>
<div ng-controller='myController' ng-init='load()'></div>
</div>
I prefer putting this kind of code in the app.run() function of angular.
e.g.
angular
.module('testApp', ['someModule'])
.constant('aConstant', 'hi')
.config(function($rootProvider) {/*some routing here*/})
.run(['$window', function($window) {
$window.onload = function() {/*do your thing*/};
}]);
also check this nice post that depicts the order that some things happen in angular
AngularJS app.run() documentation?
the following should work:
jQuery(function(){ /** my window onload functions **/ })
since angular uses a subset of jquery anyways you also may include the real thing.
better yet:
Instead of using this, you may consider using the angular way of initialising things:
that would be: http://docs.angularjs.org/api/ng.directive:ngInit
< any ng-init="functionInController(something)"...
to make it invisible until init: http://docs.angularjs.org/api/ng.directive:ngCloak
< any ng-cloak .....
to initialise/customize whole parts: http://docs.angularjs.org/guide/directive
< any directive-name....
Try
angular.element($window).bind('load', function() {
});
It's my first post in here and I haven't yet figured out to format my post properly yet, but here it goes.
So basically I can only get my code to work if i point directly to a php-file. If I try to call a method within my controller, nothing seems to happen.
My JavaScript:
$(document).ready(function() {
$(".guide_button").click(function(){
var id = $(this).text();
var data = {};
data.id = id;
$.getJSON("/guides/hehelol", data, function(response){
$('#test').text(response.id);
});
return false;
});
});
My markup:
<div id="content_pane">
<ul>
<li>RL</li>
<li>LG</li>
<li>RG</li>
<li>SG</li>
<li>GL</li>
<li>MG</li>
</ul>
</div>
<div class="description">
<h3>Description</h3>
<p id="test">This text area will contain a bit of text about the content on this section</p>
</div>
My Controller:
<?php
class Guides extends CI_Controller {
public function Guides()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
public function index()
{
$this->load->view('guides_view');
$title = 'Some title';
}
public function hehelol() //The controller I am desperatly trying to call
{
$id = $_GET['id'];
$arr = array ('id'=>$id);
echo json_encode($arr);
}
}
It might be my controller I have done something wrong with. As it is the code only works if create a hehelol.php file and refer to it directly like this.
$.getJSON("hehelol.php", data, function(response){
$('#test').text(response.id);
});
Anyone who knows what I need to do to make my controller work properly? Help please! :)
i just put your exact code in its entirety in my codeigniter app and it worked for me. Meaning I used this: ...$.getJSON("/guides/hehelol",...
Because you are making a $_GET request, you have to enable query strings.
In your config.php file, make sure this line is set to TRUE:
$config['allow_get_array']= TRUE;