Laravel 4 pages - laravel

I have a question. How could I create subpages (something like this: character.php?name=Xar) but I want it in Laravel. Do I have to create routes? Also to mention, when I create a route like this:
Route::get('account/test', 'HomeController#test');
and the view is in folder under views/aac/test, and the function is like:
public function test()
{
return View::make('aac.test');
}
it won't load the CSS. it's just an HTML page.
back to the problem again, how could I create sites like that? I'm also using Blade templating engine.

// app/routes.php
Route::get('characters', 'CharactersController#all');
Route::get('characters/{name}', 'CharactersController#detail');
// app/controllers/CharactersController.php
class CharactersController extends BaseController
{
public function all()
{
// show all characters
}
public function detail($name)
{
// find character by name & show detail for example
return View::make('acc.test');
}
}
// app/views/acc/test.blade.php
// HTML::style('css/style.css') loads CSS file located at public/css/style.css
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
{{ HTML::style('css/style.css') }}
</head>
<body>
</body>
</html>
Search function
Place search form somewhere in your view file
<form action="{{ URL::action('CharactersController#search') }}" method="get">
<input type="text" name="search-term">
<input type="submit" value="Search">
</form>
As specified, search form is submited to CharactersController and its search method.
Controller's method
public function search()
{
$name = Inpute::get('search-term');
$searchResult = Character::where('name', '=', $name)->get();
....
}
Register new route
Route::get('characters/search', 'CharactersController#search');

Related

Having trouble getting Dropzone to work with Laravel

Using Laravel Framework 6.18.0
I'm sure this is something simple but i can't seem to figure it out. I don't see the function in the controller being called to actually do the work of moving the file to the server. I've attached a link to the picture below as dropzone seems to be doing its job but the route/controller side seems to be failing.
My routes:
Route::get('/uploadpics', 'UploadPicController#index');
Route::post('/upload','UploadPicController#uploadFiles');
uploadpics.blade.php:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="{{asset('js/plugins/dropzone/dist/min/dropzone.min.css')}}">
<script src="{{asset('js/plugins/dropzone/dropzone.min.js')}}" type="text/javascript"></script>
</head>
<body>
<div class="content">
<form method="post" action="{{url('/upload')}}" enctype="multipart/form-data" class="dropzone" id="dropzone">
#csrf
</form>
</div>
<script>
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone(".dropzone",{
maxFilesize: 3, // 3 mb
acceptedFiles: ".jpeg,.jpg,.png",
});
</script>
</body>
</html>
My UploadPicController.php
<?php
namespace App\Http\Controllers;
use App\Coin;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Carbon\Carbon;
class UploadPicController extends Controller
{
public function index(){
return view('pages.uploadpics');
}
public function uploadFiles(Request $request){
dd("i'm in upload"); //**I never get here...**
if($request->hasFile('file')) {
// Upload path
$destinationPath = 'images/';
// Create directory if not exists
if (!file_exists($destinationPath)) {
mkdir($destinationPath, 0755, true);
}
// Get file extension
$extension = $request->file('file')->getClientOriginalExtension();
// Valid extensions
$validextensions = array("jpeg","jpg","png");
// Check extension
if(in_array(strtolower($extension), $validextensions)){
// Rename file
$fileName = str_slug(Carbon::now()->toDayDateTimeString()).rand(11111, 99999) .'.' . $extension;
// Uploading file to given path
$request->file('file')->move($destinationPath, $fileName);
}
}
}
}
I can use dropzone on the page with no errors but it does not copy the file to the server-- it's like the function in the controller never gets called (see the dd(); - never hits).
enter image description here

Why can I not use a body class selector with Laravel Dusk?

I am using Laravel 5.5 and Dusk 2.0. I have the following html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body class="my-body-class" id="my-body-div">
<div class="my-content-class" id="my-content-div">
Content goes here.
</div>
</body>
</html>
Here's my Dusk test.
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/test/admin-fixed-layout');
$this->assertNotNull($browser->element('.my-content-class'));
$this->assertNotNull($browser->element('#my-content-div'));
// $this->assertNotNull($browser->element('.my-body-class'));
$this->assertNotNull($browser->element('#my-body-div'));
});
}
If I un-comment the assertion that uses the body class selector, the test fails. Why?
This is because by default prefix is set to body:
public function __construct($driver, $prefix = 'body')
{
$this->driver = $driver;
$this->prefix = trim($prefix);
}
in Laravel\Dusk\ElementResolver class.
If you really need to change this (but probably there is no point), you can add the following method into Tests/DuskTestCase class:
protected function newBrowser($driver)
{
return new \Laravel\Dusk\Browser($driver, new \Laravel\Dusk\ElementResolver($driver, ''));
}
This will override default browser and pass empty prefix instead of default body prefix

What is the proper way of setting the page meta title in laravel

In many examples and documents I generally see that the page title is set via someController->main.blade.php->somePage.blade.php. Something like:
SomeController.php
public function someAction()
{
$title = 'Some Title';
return view('somePage', ['title'=>$title]);
}
main.blade.php
<head>
<title> #section('title') | Page #show </title>
...
somePage.blade.php
#section ('title')
{{$title}} #parent
#endsection
Wouldn't it be mode convenient to set it directly/only over the controller and blade layout file? I mean something like:
SomeController.php
public function someAction()
{
$title = 'Some Title';
return view('somePage', ['title'=>$title]);
}
main.blade.php
<head>
<title>{{ $title }}</title>
...
Wouldn't it be better to use it in that way?
I prefer not to assign the title from the controller - it's content and should be in the template from my point of view. I like to have a section in the template like
//layout file
<title> FancyApp - #yield('title')</title>
// Template
#section('title', 'Page Title')

Laravel 4 search function

From my previous question, I've gotten this code from a user.
// app/routes.php
Route::get('characters', 'CharactersController#all');
Route::get('characters/{name}', 'CharactersController#detail');
// app/controllers/CharactersController.php
class CharactersController extends BaseController
{
public function all()
{
// show all characters
}
public function detail($name)
{
// find character by name & show detail for example
return View::make('acc.test');
}
}
// app/views/acc/test.blade.php
// HTML::style('css/style.css') loads CSS file located at public/css/style.css
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
{{ HTML::style('css/style.css') }}
</head>
<body>
</body>
</html>
also, the search function:
<form action="{{ URL::action('CharactersController#search') }}" method="get">
<input type="text" name="search-term">
<input type="submit" value="Search">
public function search()
{
$name = Input::get('search-term');
$searchResult = Character::where('name', '=', $name)->get();
....
}
Route::get('characters/search', 'CharactersController#search');
how could I in the :
public function detail($name) { // find character by name & show detail for example return View::make('acc.test'); }
how could I find the character by name? I've tried doing something like
$name = $player->name
(I have a model called players, I've also changed Character::where to Player::where), what do I have to insert there? also, how could I display it in the view?
So I mean when I search a player by name it displays the players name ($player->name) for every specific player.
Also do I have to change the relations in the model toHasMany or something like that?
You can do it this way
public function detail($name) {
$player = Player::where('name', '=', $name)->first();
}
If more players can have same name I would rather pass ID instead of name, so in this case use this solution
public function detail($id) {
$player = Player::findOrFail($id);
}

Laravel 4 content yielded before layout

I am using a fresh build today of Laravel 4.
I have a dashboardController
class DashboardController extends BaseController {
protected $layout = 'layouts.dashboard';
public function index()
{
$this->layout->content = View::make('dashboard.default');
}
}
I have a simple route
Route::get('/', 'DashboardController#index');
I have a blade layout in views/layouts/dashboard.blade.php
For the sake of saving everyone from all of the actual HTML ill use a mock up.
<html>
<head>
<title></title>
</head>
<body>
#yield('content')
</body>
</html>
I have a default blade file in views/dashboard/ that has the following (edited for simplicity)
#section('content')
<p>This is not rocket science</p>
#stop
For some reason the content gets generated before the layout.
I am using a different approach to set the layouts globally to routes using a custom filter. Put the following filter into the app/filters.php
Route::filter('theme', function($route, $request, $response, $layout='layouts.default')
{
// Redirects have no content and errors should handle their own layout.
if ($response->getStatusCode() > 300) return;
//get original view object
$view = $response->getOriginalContent();
//we will render the view nested to the layout
$content = View::make($layout)->nest('_content',$view->getName(), $view->getData())->render();
$response->setContent($content);
});
and now instead of setting layout property in the controller class, you can group the routes and apply the filter as shown below.
Route::group(array('after' => 'theme:layouts.dashboard'), function()
{
Route::get('/admin', 'DashboardController#getIndex');
Route::get('/admin/dashboard', function(){ return View::make('dashboard.default'); });
});
When creating the views, make sure to use the #section('sectionName') in all the sub views and use #yield('sectionName') in the layout views.
I find it easier to do my layout like this for example. I would create my master blade file like so
<html>
<body>
#yield('content');
</body>
</html
And in the blade files that I want to use the master at the top i would put
#extends('master')
then content like so
#section('content')
// content
#stop
Hope this helps.
When you use controller layouts, i.e. $this->layout->..., then you get access to data as variables, not sections. So to access content in your layout you should use...
<html>
<head>
<title></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
And in your partial, you would not use #section or #stop...
<p>This is not rocket science</p>

Resources