Laravel wild card problems - laravel

I have master.blade.php as one of my layouts. When I load post.blade.php in browser without a wildcard like url:localhost8000/post it works and full required page is loaded but when I use a wild card url:localhost8000/post/1 the page is not completely loaded. Only the footer is displayed. Header and body appears white fire bug shows the data is present which is passed through the wildcard.
Both the pages should appear as same because the difference is only of wildcard in url.
My route file is
Route::get('post','PostsController#index');
Route::get('post/{post}','PostsController#show');
Here is my PostsController
class PostsController extends Controller{
public function index(){
return view('posts.post');
}
public function show(Post $post){
return view('posts.post',compact('post'));
}
}

When this kind of error happens, there is most likely a PHP variable error in your project.
A way to find out is to view source of the HTML and look at the first or last part of the page depending on where the error is occurring.
Also, I assume you have properly bound the keyword 'id' in your routes, in your Post model.

You should pass id instead of object because your url is passing an integer localhost:8000/post/1
class PostsController extends Controller{
public function index(){
return view('posts.post');
}
public function show($id){
$post = Post::find($id);
return view('posts.post',compact('post'));
}
}

if u include some assets files like CSS or JS files to the HTML document the file path should start with a forward slash for example instead of 'assets/css/foo.css' you should write '/assets/css/foo.css' it worked for me when all the document was loading but the footer didn`t

Related

laravel route issue .the page you are looking for could not be found

I want to show data from the database when I press the button in one view it displays me the specific data by the id in the other view this is my first view.
In my controller:
public function show($id) {
$symptoms=symptoms::all();
foreach ($symptoms as $symptom) $symptom=symptoms::find($id);
return view('front.second',compact('symptom'));
}
My problem is when displays the second view it can't find the page and the route just display the id not the method/id :(
Try this:
public function show(symptoms $symptom)
{
return view('front.second', compact('symptom'));
}
Or:
public function show($id)
{
$symptom = symptoms::find($id);
return view('front.second', compact('symptom'));
}
And read Laravel's Controllers Documentation carefully.
As you shared image, is clearly shows that you are using below route
{{route('show',['id'=>111])}}
Check your route by php artisan route:list and use right one, I guess you should use like
{{route('user.show',['id'=>111])}}
If you Controller name is UserController in resource

Codeigniter Changing routes makes $this->uri->segment() not working

I've altered routes.php
$route['category']='Home/category';
for making url look like www.website.com/category insteead of www.website.com/Home/category. Since Home is my default controller.
but if i am using $this->uri->segment(); inside category function, its not working. this is my controller
class Home extends CI_Controller {
public function category()
{
$value=$this->uri->segmet(3);
}
}
And my url is www.website.com/category/books
I am getting result if I dont alter routes. But by altering routes, I need this to work. Please help me. Thanks
You can debug what you have there by:
var_dump($this->uri->segment_array());
this will give you array of all segments in URI.
Also you can try to debug with this method:
var_dump($this->uri->rsegment_array());
this will give you array of all routed segments in URI
Respectivelly, you can use $this->uri->segment() or $this->uri->rsegment() what ever you find more appropriate for your application.
hello please check segment spelling in your code
class Home extends CI_Controller {
public function category()
{
$value=$this->uri->segmet(3); //wrong
$value=$this->uri->segment(3);
}
}
First of all you need to load URL library and than if your URL is:
www.website.com/category/books
And you want to get books from URL than segment should be:
$value=$this->uri->segment(2); //books
My URL libraries are in autoload. Anyway I solved it. I just configured my routes like this
$route['category/(:any)']='Home/category/$1'

Controller method not called in laravel 4

I'm trying to learn laravel 4. I created a form(using view) and returned it via a controller(testController) using index method. I had created this controller using artisan command.
i created another method (dologin) in the controller which would process the form. In the form url parameter i gave the address of dologin method.
This is the route:
Route::resource('test', 'testController');
This is the controller
<?php
class testController extends \BaseController {
public function index()
{
return View::make('test.index');
}
public function dologin(){
echo "working";
}
and this is the index view file
{{ Form::open(array('url'=>'test/loginform')) }}
{{ Form::text('username', null, array('placeholder'=>'Username')) }}<br/>
{{ Form::password('password', array('placeholder'=>'Password')) }}<br/>
{{ Form::submit('Login') }}
{{ Form::close() }}
After submitting form, it should echo "working" in the browser. But after submitting the form, page is blank. The url changes though from
/laravel/public/index.php/test/
to
/laravel/public/index.php/test/loginform
umefarooq's answer is correct, however hopefully this answer should give you a bit more insight into getting a head-start in your Laravel development as well as a consistent best-practice programming style.
Firstly, class names should really start with a capital letter. Try to keep methods / function names starting with a lower case letter, and class names starting with a capital.
Secondly, you don't need the \ in front of BaseController. You only need the backslash if you are name-spacing your controller. e.g. if your controller is in the folder Admin\TestController.php, and you put your TestController in the Admin namespace by typing <?php namespace Admin at the beginning of the file. This is when you should use \BaseController because you are telling your TestController to extend BaseController from the Global Namespace. Alternatively, before you declare your class, you can type use BaseController; and you don't need to put a \ in every time.
Specifically related to your question:
When you use resource routes in your routes file, you are telling Laravel that the controller can have any or all of the following methods: index, show, create, store, edit, update and destroy.
As such, Route::resource('test', 'TestController'); will point to TestController.php inside your controllers folder.
Your TestController should be structured as follows, most restful controllers will use the below as some kind of boilerplate:
<?php
class TestController extends BaseController
{
public function __construct()
{
}
// Typically used for listing all or filtered subset of items
public function index()
{
$tests = Test::all();
return View::make('test.index', compact('tests'));
}
// Typically shows a specific item detail
public function show($id)
{
$test = Test::find($id);
return View::make('test.show', compact('test'));
}
// Typically used to show the form which creates a new resource.
public function create()
{
return View::make('test.create');
}
// Handles the post request from the create form
public function store()
{
$test = new Test;
$test->attribute1 = Input::get('attribute1');
$test->attribute2 = Input::get('attribute2');
$test->attribute3 = Input::get('attribute3');
$test->attribute4 = Input::get('attribute4');
if ($test->save())
{
return Redirect::route('test.show', $test->id);
}
}
// Shows the edit form
public function edit($id)
{
$test = Test::find($id);
return View::make('test.edit', compact('test'));
}
// Handles storing the submitted PUT request from the edit form.
public function update($id)
{
$test = Test::find($id);
$test->attribute1 = Input::get('attribute1');
$test->attribute2 = Input::get('attribute2');
$test->attribute3 = Input::get('attribute3');
$test->attribute4 = Input::get('attribute4');
if ($test->save())
{
return Redirect::route('test.show', [$id]);
}
}
// Used to delete a resource.
public function destroy($id)
{
$test = Test::find($id);
$test->delete();
return Redirect::route('test.index');
}
}
Also, the beauty of using Resource Controllers is that you can take advantage of named routes.
in the terminal window, type in php artisan routes.
You should see 7 named routes.
test.index
test.destroy
test.show
test.edit
test.destroy
test.create
test.update
So within your form, instead of doing
{{ Form::open(array('url'=>'test/loginform')) }} you can point the url to a named route instead:
{{ Form::open(array('route' => array('test.store')) }}
That way if you ever change the url, or need to move around your site structure, this will be easy, because the forms post url will auto bind to the named route within the routes file. You wont need to update every single one of your views to ensure that the url's are pointing to the correct location.
Finally, as a starting point, I would recommend using JefreyWay/Laravel-4-Generators package. https://github.com/JeffreyWay/Laravel-4-Generators . Use them to create your resources, controllers, views etc. and see how the generators scaffold your models, views and, controllers for you.
Here is another resource to help you get started:
https://laracasts.com/lessons/understanding-rest
Route::resource('test', 'testController');
will work for RESTful method of controller, like index, edit, destroy, create and now you are using custom method of controller for this you need to create another route
Route::post("test/loginform",'testController#dologin');
hope this will work for you. read route documentation http://laravel.com/docs/routing
In addition to what umefarooq said, which is 100% accurate. You need to look into flash messages as well.
public function dologin(){
//do login verification stuff
If login validated
Return redirect::to(logged/page)->with('message', 'You're logged in');
If login failed
Return redirect::to('test')->with('message', 'You login credentials fail');
}
For further research:
http://laravel.com/docs/responses

Laravel views do not output anything

I'm new to PHP Laravel framework. I'm studying it and playing with simple examples of code. My problem is that my views do not output anything - a blank white screen appears when I try to reach controller methods, for example, localhost/my-application/cms/action1
My routes file:
Route::controller('cms', 'CmsController');
My controller:
class CmsController extends BaseController {
public function getIndex()
{
View::make('cms.index');
}
public function getAction1()
{
View::make('cms.action1');
}
public function getAction2()
{
View::make('cms.action2');
}
}
My views are located in views/cms. They are very simple, for example:
<h1>Action1</h1>
<?php echo 'this is Action1'; ?>
And these views do not output anything, just simple blank white page appears. I tried to:
1) rename views files, and Laravel threw exception - "view not found", or so.
2) move view::make() methods to Routes file - the views were displayed then.
So where is the problem?
The bootstrap index.php file in laravel is inside the public folder.
So unless you've created a vhost for your application, you have to access it like
localhost/my-application/public/cms/action1
EDIT
Forget it. The problem is that you do not return the view::make from each function.
return View::make('cms.index');

Codeigniter common templates

Let's say that I have a website that has 100 different pages. Each page uses a common header and footer. Inside the header is some dynamic content that comes from a database.
I'd like to avoid having to have code in every single controller and action that passes this common code into the view.
function index()
{
// It sucks to have to include this on every controller action.
data['title'] = "This is the index page";
data['currentUserName'] = "John Smith";
$this->load->view("main_view", data);
}
function comments()
{
// It sucks to have to include this on every controller action.
data['title'] = "Comment list";
data['currentUserName'] = "John Smith";
$this->load->view("comment_view", data);
}
I realize that I could refactor the code so that the common parts are in a single function and the function is called by the action. Doing so would reduce SOME of the pain, but it still doesn't feel right since I'd still have to make a call to that function every time.
What's the correct way to handle this?
One way I have been doing this is to extend the default controller class. You can read up on extending classes with MY_Controller in the user guide. Inside this extended class you can include something that you ALWAYS want to do, like render the page header template before the main content, or authorise a users access etc.
class MY_Controller extends Controller {
function __construct()
{
parent::Controller();
//code to always do goes here
echo 'Always print this comment';
$this->load->view('partials/template_start');
}
}
Then you can have your normal controller class extend THIS class by using
class MyControllerNameHere extends MY_Controller {
function __construct()
{
//setup here
}
function index()
{
echo 'Only print this bit when this method is called';
$this->load->view('partials/MYPAGENAMEHERE');
}
}
There are other ways of doing this, I use a mixture of the above and William's Concepts Codeigniter Template library. Do a bit of searching - there are a few solutions for you.
I had a similar situation. I created an 'includes' folder, and in there put a file that had the repetitive code from my controllers. Then in the controllers just include('/path/to/includeFile.php');
Don't know if it's the "correct" way, but it works well for me.
I ran across this after a search of their site. http://codeigniter.com/wiki/Header_and_footer_and_menu_on_every_page/ I'll review this page and its links, then post my thoughts.

Resources