Laravel framework index.php - php

I have a project in laravel5 and I have directed virtual host on /public directory. Where should be my main page index.php now? There is some index.php in public folder already but it contains some content that I dont understand. So where should be my index now? Maybe in views?

If you are using Laravel 5.1 then your initial page should be this resources/views/welcome.blade.php.
You can change it to any name. But also you should change the same in your Controller.
Note : If you are rendering the view through controller then you should have the file name like this yourfilename.blade.php
Your views should always inside resources/views/*

The index file stays at the same place, to call a new file that you made, could be with HTML, you can put that file in the view and call it from the controller, hope this is the answer you are looking for.

When a request come to your server, the first file that it is executed is index.php. But this is not the file shown. In Laravel you don't have to force any file be named index.php. So let's imagine that you are trying set up a new index file. You have to use routes.php
routes.php
Route::get("/" , "HomeController#index");
HomeController.php
function index(){
return view("home/index");
}
views/home/index.blade.php
<html>
<head>
</head>
<body>
<p>Index page</p>
</body>
</html>
When a GET request to "/" it's done, the index function of the HomeController it's executed and the view index.blade.php, stored in views/home it's shown.
This it's the basic behaviour of Laravel. So you mustn't rename or move the index.php file in public folder.

Related

include function in codeigniter

Recently I have started learning codeigniter framework. I am facing a problem in including files from another folder. In my view folder, i have two folders named patient and includes. This is like below
views
includes - it has two folders (front & back) - back folder has a file head.php.
patients - it has a file patient.php
now I want to include head.php in patient.php.
when I use (include 'D:\XMPP\htdocs\codeigniter\application\views\includes\back\head.php'), it works.
but when I use (include '../includes/back/head.php'), it doesn't work.
how to make the 2nd option work?
Thank you.
try this route - includes\back\head.php
<?php include VIEWPATH . "includes\back\head.php"; ?>

Returning javascript view in Laravel

In Ruby on Rails In my controller I can do something like this:
def jstest
respond_to do |format|
format.js
end
end
This would return jstest.js.erb from the relevant views directory
jstest.js.erb
alert("Hello World")
The result being I get hello world alert.
I want to do the same thing in Laravel
In my controller I have:
public function jstest( )
{
return view('jstest');
}
in my view (which I have tried with both jstest.blade.js and just jstest.js)
but I get the error, view cannot be found.
The only way I get this to work is by calling the view jstest.blade.php and including my js in a <script> tag within this php file. but this feels a bit wrong...?
Is this even possible in Laravel? If so, where am I going wrong?
Example use case:
Imagine the following example, I have a table of comments, a user can click a delete button which will send an ajax request to delete the comment.
My Route:
Route::delete('post/comments/{comment}','commentsController#delete');
In my controller:
Public Function delete($comment)
{
$comment->delete();
return commentDeleted.js
}
commentDeleted.js:
$(#comment).remove();
So from the users perspective, they click delete and the comment disappears from their screen without loading a new page.
I don't know if Laravel supports this out of the box, but this is my take on your issue.
This is how I understood your problem: you want to specify a path to .js file as an argument to a function and you want this .js file to be immediately executed when loaded via browser.
I'd do it in these 2 steps
Create a jsview() function that accepts a path, similar to view()
Create the base .blade.php file which will be used by the above
The .blade.php contents
Assumption is that this view will be named javascript.blade.php and it's in resources/views/ directory.
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="{{$path}}"></script>
</head>
<body></body>
</html>
The function
function jsview($path)
{
return view('javascript', ['path' => $path]);
}
Usage
<?php
Route::get('/test', function()
{
return jsview('/assets/my.js');
});
This answer isn't really fully complete because your browser will be loading files from public/ directory so all .js files you specify this way would be read from there. I believe Laravel does have something related to resources, but I'm not expert enough on that matter.
This is posted only to outline how to achieve something that you need, but isn't available out of the box.

CodeIgniter: My CSS file in my view not working

hallo I am running CodeIgniter 3.0.6 and this is my directory structure:
/application
/assets
/js
/mycss.css
/system
My default controller is loading this file in the index.php just fine but when i load another page, the whole page loads afresh but although this file is getting loaded, it is not doing what i want it to do. When i view sourcepage, the links are OK. What could be the problem. I am using base_url('assets/js/mycss') to load it
To put it simpler i have my default controller as welcome. In the index() method of this controller i have $this->load->view('template/index'). This works fine. Now if i load the same page in another method, uniserve(), in the same controller my css file does not do what i want it to do (my page layout is distorted) but all other css files are doing well. What is the problem here. Kindly help.
This link provide exact problem and exact code but my beyond.min.css is not working after it loads fine. How can i achieve the same solution provided in php's codeigniter? Thank you:
Why can't I load js and css files on my pages?
You could also use link_tag().
<?= link_tag('assets/js/mycss.css') ?>
will give
<link href="http://yoursite.com/assets/js/mycss.css" rel="stylesheet" type="text/css" />
And make sure that base_url is specified in your config.php file

Codeigniter doesn't load view correctly

I have a codeigniter project that is already up and running perfectly on the client server, now after I changed my laptop and installed xammp 1.8.2 the project is not working correctly,
the index page works just fine, and when I try to log in it only displays these two lines
load->view('header');?>
load->view('side_menu'); ?>
These two lines are the only php code in the view Home.php ,so the problem is that load view function in the controller loads the view "Home.php" but only displays the php code
The hierarchy is:
application
controller
main_controller.php
view
home.php
and attached a screen shot of the displaying
Could any one help me with that???
Thanks in advance
Given your example, You will need to open your php document with <?php and end it with ?> (not on each line as you have it.)
You will also need to use $this from the controller class.
<?php
$this->load->view('header');
$this->load->view('side_menu');
?>

PHP directory not correct

I have a /controller/controller.php file that runs this code:
include('../model/model.php');
print page_load();
The model/model.php loads correctly and the page_load() function gets loaded. However, the model.php file still 'thinks' that it is in the controller directory. So if I try and do any POST actions in the model.php, it looks in the controller directory rather than the model directory.
Can I please have some advice on how to fix this?
thanks
In model.php your need set action in form with correct path to post it to your model.php
<form action="/model/model.php">
// bla-bla-input-bla-bla
</form>
You are still inside the controller page [1]. As suggested by the name the controller page simply includes the the model page. What Sergey said is partly correct. You need to to assign a more complete path to the form's action parameter. As I am sure you will see Sergey's answer does not work. This is because you need to go up one directory. This is done by including two dots that is .. before the path.
<form action="../model/model.php">
You are already including the .. in your include statement. Just use it in the same way.
Please let us know if you need more help. :-)
[1] http://php.net/manual/en/function.include.php
I think you are looking for chdir function.
With it, you can change your current directory, allowing PHP to think you are in model/ folder.

Categories