Laravel views do not output anything - laravel

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');

Related

How to work with subdirectory controllers in CodeIgniter 4?

I need help with using sub directory controllers in CodeIgniter 4.
I just can't make it work for some reason.
This is the URL for example: www.example.com/admin/dashboard
In the controllers folder, I created a folder named Admin, and a file named Dashboard.php.
I used this code in Dashboard.php:
namespace App\Controllers;
class Dashboard extends BaseController
{
public function index()
{
}
}
I tried changing the class name to AdminDashboard, Admin_Dashboard, pretty much every logical name but every time I get a 404 error, saying:
Controller or its method is not found:
App\Controllers\Admin\Dashboard::index
I know the file itself gets loaded successfully, but I think I don't declare the classname correctly and it keeps throwing me those 404 errors.
The documentation of CI4 isn't providing any information about what the classname should be called unfortunately...
UPDATE #1
I managed to make it work by changing few things:
namespace App\Controllers\Admin;
use CodeIgniter\Controller;
class Dashboard extends Controller
{
public function index()
{
}
}
But now it won't extend the BaseController which has some core functions that I built for my app.
Any ideas to how to make it extend BaseController?
I must admit that I don't have much knowledge about namespacing yet, so that might be the reason for my mistakes.
As I imagined, the problem was that I didn't learn about namespacing.
I needed to point the use line at the BaseController location.
namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class Dashboard extends BaseController
{
public function index()
{
}
}
Now www.example.com/admin/dashboard/ goes directly to that index function, as intended.
php spark make:controller /Subfolder/ControllerName
$routes->add('/(.+?)_(.+?)/(.+?)$', 'subdir\\\\$1_$2::$3');
$routes->add('/(.+?)_(.+?)$', 'subdir\\\\$1_$2::index');
I was able to map with this setting.
The route mapping could be as simple as:
$routes->group('admin', static function ($routes) {
$routes->get('dashboard', 'Admin\Dashboard::index');
});

Laravel wild card problems

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

Laravel frontend and backend with different multilanguage

In Laravel at the same time i need have different language/locale in site frontend and backend(administration). Frontend need 4 languages(en,de,fr,it), backend need 3 languages(en,lt,es).
Example: In browser i have two open tabs - 1 tab frontend (lang: de), 2 tab backend (lang: en). How to do it ? with setLocale? or i need different array for example backend?
One way you can easily handle this is by creating two BaseController classes for your frontend and backend controllers.
You can then set different languages for your frontend and backend from the right BaseController constructor using App::setLocale method.
Example:
<?php
class FrontendBaseController extends Controller
{
public function __construct()
{
App::setLocale(Session::get('frontend-locale'));
}
}
class BackendBaseController extends Controller
{
public function __construct()
{
App::setLocale(Session::get('backend-locale'));
}
}
class HomeController extends FrontendBaseController
{
public function __construct()
{
}
}
class BackendDashboardController extends BackendBaseController
{
public function __construct()
{
}
}
In the above example, I'm retrieving the current locale from the session.
You can put your language files in the app/lang folder. I will suggest you to have separate folders for your frontend and backend language files.
Example folder structure:
/app
/lang
/en
backend/
dashboard.php
frontend/
home.php
/de
backend/
dashboard.php
frontend/
home.php
Sample content of app/lang/en/backend/dashboard.php:
<?php
return array(
'welcome' => 'Welcome to Backend!'
);
You can output the value of welcome key for example with
echo Lang::get('backend/dashboard.welcome');.
I hope you got the idea. For more details, feel free to check out the official documentation.
Instead of opening two different tabs in the same browser, perhaps you should consider opening two different browser sessions, to avoid that the backend- and frontend-sessions with different language settings overwrite each other.

CodeIgniter Undefined Variable while trying to create subview

Hi I am new to CodeIgniter and am taking a tutorial. I ran into an error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: subview
Filename: admin/_layout_modal.php
Line Number: 7
An Error Was Encountered
Unable to load the requested file: .php
I followed the tutorial to the tee and I cant seem to find out where I am going wrong.
here is the controller
<?php
class User extends Admin_Controller {
public function __construct() {
parent::__construct();
}
public function login() {
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal', $this->data);
}
}
and the view
<?php $this->load->view($subview); ?>
Please help...
If you gave script from your .php file not tutorial then everything is ok.
You are probably typing the wrong url in when you try to access the page. Make sure you are loading "admin/user/login" instead of "admin/dashboard/modal".
If you are following tutplus tutorial- building cms with codeigniter, First watch two episodes, Managing User Part 1 and Managing User Part 2 and then Start building, Your question will be answered on Part 2.... the dashboard has to declare the subview variable and
CREATE THAT admin/dashboard/index.php file in views
class Dashboard extends Admin_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->data['subview'] = 'admin/dashboard/index';
$this->load->view('admin/_layout_main',$this->data);
}
public function Modal(){
$this->load->view('admin/_layout_modal',$this->data);
}
}
don't need to pass data to $this. Just pass the data to
$data['variable_name']=data;
pass this $data variable to view through load class.

Showing a view in Zend Framework

I've never worked with Zend Framework before, but I've worked with others (CodeIgniter, Kohana, etc). Right now I was asked to just show a view that wasn't existing so I started looking into the documentation and examples I could find and I've always found examples that use the Model part of the MVC, but in this case I just need to load a view and I can't figure out how to do that. I have this:
File: "BookController":
require_once("Initiate.php");
class BookController extends Initiate {
public function init() {
parent::init();
}
public function bookAction(){
#$client = Zend_Auth::getInstance()->getIdentity();
$view = new Zend_View();
echo $this->view->render('book.phtml');
#$this->view->assign("book", $client);
#echo $this->view->render('book.phtml');
}
the view is called "book.phtml" and is found in /application/views/scripts/bookapi/
What am I missing?
Given that eveything is setup correctly with MVC, your Controller should extend Zend_Controller_Action:
class BookController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->funnyText = 'This is a funny text.';
}
}
Then, in your application/views/scripts/book/ folder, there has to be a index.phtml. Which could look like that:
<p>
<?php echo $this->funnyText; ?>
</p>
That's it, nothing more required.
Btw. doesn't make sense to have a controller called book and then also an action called book
you don't have to manage view yourself there is an Action Controller Helper called View Renderer it does your job for you all you need to follow is its naming convention i.e if your controller name is 'BookController' then its view file should be located at views/scripts/book/boo.phtml .
Assuming BookController extends Zend_Controller, it should automatically setup the view and you shouldn't have to render it. Your view file should be in /application/views/scripts/book/book.phtml. Follow the quick start for more information.

Resources